Skip to content

First TDS connection

Log in with FreeTDS, write two rows and check them from PostgreSQL. Configure and verify an encrypted connection.

Log in and write two rows

After initialization, run this in the local client shell. FreeTDS tsql prompts for bbf_admin’s password. -H and -p select the TCP endpoint. -D master selects a logical database at login. The 1> prompt means the client accepts a new batch.

TDSVER=7.4 tsql -H 127.0.0.1 -p 1433 -U bbf_admin -D master

Enter the following at the tsql prompt in the fresh example database. GO on its own line sends the batch. exit returns to the shell. Expect master, followed by n = 2 and total = 42. Use an application database for real tables. Here master makes the example reproducible immediately after initialization.

SELECT DB_NAME() AS current_db;
GO
CREATE TABLE dbo.mimus_probe (id int PRIMARY KEY, amount int NOT NULL);
GO
INSERT INTO dbo.mimus_probe VALUES (1, 40), (2, 2);
SELECT COUNT(*) AS n, SUM(amount) AS total FROM dbo.mimus_probe;
GO
exit

From the same shell as the OS account postgres, check the rows through native psql. Expect wire | 2 | 42. The TDS session wrote to dbo in logical master. PostgreSQL sees the table in physical schema master_dbo.

psql -X -d wire -c 'SELECT current_database(), count(*), sum(amount) FROM master_dbo.mimus_probe;'

Address, authentication and client settings

ParameterBehavior in this build
Server address and portThe TDS bind code reads PostgreSQL listen_addresses. Although babelfishpg_tds.listen_addresses is registered, setting it alone does not restrict this listener. babelfishpg_tds.port defaults to 1433. Restart for a binding change.
Database and userHBA matches the physical babelfishpg_tsql.database_name, source address and login. The driver sends the logical database (JDBC databaseName). HBA uses the first matching rule. Reload changes and reconnect. Check both IPv4 and IPv6 when using hostnames.
Password loginThe password and md5 HBA branches verify the password from LOGIN7 against the PG role’s stored verifier, including SCRAM verifiers. A scram-sha-256 HBA method is rejected. trust skips verification. Use TLS for network password logins.
Integrated loginThe gss path needs a GSS-enabled build and working Kerberos credentials. NTLMSSP is rejected. SSPI as an HBA method, peer, ident, LDAP, PAM, BSD and certificate login are unsupported in the TDS handler. OAuth login returns an authentication error. The exercised login used a password.
Wire version and packetsThe server default babelfishpg_tds.tds_default_protocol_version = DEFAULT follows the client. The configured choices run through TDS 7.4. The walkthrough uses 7.4. Default packet size is 4096 bytes. A nonzero client request must be 512–32767 bytes and sizes are rounded down to a multiple of four. The server answers the MARS option with zero. Use separate connections for overlapping active results.

Upstream TDS listener · Login and PRELOGIN handling · TDS settings

Verify TLS on the actual session

Use an OpenSSL-enabled Mimus build. As the server OS owner, install the server certificate and private key, readable by PostgreSQL with the usual private-key permissions. The certificate must cover the hostname the client uses. For this example it covers localhost. Configure the server in postgresql.conf as follows.

ssl = on
ssl_cert_file = 'server.crt'
ssl_key_file = 'server.key'
babelfishpg_tds.tds_ssl_min_protocol_version = 'TLSv1.2'
babelfishpg_tds.tds_ssl_max_protocol_version = 'TLSv1.2'
babelfishpg_tds.tds_ssl_encrypt = on

The TDS minimum and maximum default to TLSv1 and TLSv1.2. TLS 1.3 is outside the TDS version selector. The TLS and encryption parameters are registered for reload, but the extension builds its TLS context at server startup. Restart after configuring the certificate, key or protocol bounds and establish a new session. With server SSL active, tds_ssl_encrypt = on requires full-session encryption in PRELOGIN. With the default off, a client can request full encryption, login-only encryption or no encryption according to its capabilities. The switch alone cannot supply a missing TLS context.

Replace the local exercise’s HBA line with hostssl wire bbf_admin 127.0.0.1/32 password for this IPv4-only endpoint. For remote operation, scope it to the actual client address, bind the service to the intended interface and exclude plaintext fallback rules for that login. On the client, put this profile in freetds.conf and substitute the path to the issuing CA.

[mimus-tls]
host = localhost
port = 1433
tds version = 7.4
encryption = require
ca file = /path/to/root.crt
check certificate hostname = yes

Connect as bbf_admin. In the example the server is bound to 127.0.0.1. The client reaches it as localhost and checks that hostname against its certificate.

tsql -S mimus-tls -U bbf_admin -D master

At the tsql prompt run this query. The verified FreeTDS 1.5.18 session returned 1 | TLSv1.2. With Microsoft JDBC the corresponding client settings for full encryption and certificate validation are encrypt=true and trustServerCertificate=false, with a trusted issuing CA and matching hostname. The JDBC bulk-write test used a separate loopback configuration.

SELECT ssl, version FROM pg_stat_ssl WHERE pid = @@SPID;
GO