🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Data Management / Database Administration › Database Security & Role Management › Lesson 4
TEC-DATA86-05 · Lesson 4 · 10 clock hours · demo

SSL/TLS Encryption in Transit & Certificate Management

Objective. Learners configure PostgreSQL to require SSL connections using a self-signed certificate, verify encryption is active, and explain the role of certificates in client authentication.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Why Encrypt Database Connections?

When a client application connects to a database server over a network, the data packets travel through switches, routers, and potentially untrusted segments. Without encryption, any device on that path can read usernames, passwords, queries, and result sets in plain text. This is called an on-path (or man-in-the-middle) attack. SSL/TLS wraps the connection in an encrypted tunnel so that intercepted packets are unreadable without the session keys.

Encryption in transit is different from encryption at rest. In-transit encryption protects data moving across a network. At-rest encryption (such as Microsoft's Transparent Data Encryption, covered later) protects data stored on disk. A database system needs both layers to be fully protected.

How SSL/TLS Works in PostgreSQL

PostgreSQL uses the OpenSSL library to provide TLS support. When SSL is enabled, the server presents a certificate to the client during the handshake. The certificate contains the server's public key and is signed by a Certificate Authority (CA). The client checks that signature to confirm it is talking to the right server, not an impersonator.

Three files control SSL in PostgreSQL:

These files must sit in the PostgreSQL data directory (the folder that contains `postgresql.conf` and `pg_hba.conf`). The private key file must be owned by the `postgres` OS user and have permissions 0600 (read and write for owner only). If the permissions are too open, PostgreSQL will refuse to start with SSL enabled. This is a security control, not an accident — a world-readable private key is a compromised private key.

Generating a Self-Signed Certificate with OpenSSL

In a production environment you would obtain a certificate from a trusted CA such as DigiCert or your organization's internal CA. For lab and testing purposes a self-signed certificate works just as well to practice the configuration steps. The steps are:

1. Generate a CA key and self-signed CA certificate.

2. Generate a server key and a Certificate Signing Request (CSR).

3. Sign the CSR with the CA to produce the server certificate.

4. Copy `server.crt` and `server.key` to the data directory and set ownership and permissions.

See the Worked Examples section below for the exact OpenSSL commands.

Configuring postgresql.conf and pg_hba.conf

Once the certificate files are in place, open `postgresql.conf` and set:

```

ssl = on

```

Then reload the server (`pg_ctlcluster reload` on Debian/Ubuntu systems, or `SELECT pg_reload_conf();` from a superuser session). PostgreSQL does not require a full restart for this change.

Next, edit `pg_hba.conf` to require SSL for specific connections by using the hostssl record type instead of host. A `hostssl` line accepts only encrypted connections; a plain `host` line accepts both. If a client tries to connect with `sslmode=disable` to a `hostssl` entry, the server rejects the connection. This is how you enforce encryption — not just offer it.

Example `pg_hba.conf` line:

```

hostssl all all 0.0.0.0/0 scram-sha-256

```

Verifying the Connection

After reloading, connect with psql using:

```

psql "host=localhost dbname=mydb user=myuser sslmode=require"

```

Inside the session, run `\conninfo`. The output will include the phrase SSL connection (protocol: TLSv1.3, ...) confirming the tunnel is active.

You can also verify from outside psql using the OpenSSL client tool:

```

openssl s_client -connect localhost:5432 -starttls postgres

```

This shows the full TLS handshake, the certificate chain, and the cipher suite negotiated.

To confirm enforcement, set `sslmode=disable` in the connection string. When `pg_hba.conf` uses `hostssl`, the server returns a connection-refused message — proof that unencrypted connections are blocked.

Certificates and Client Authentication

Certificates are not only for server identity. PostgreSQL supports certificate-based client authentication, where each client presents its own certificate signed by a trusted CA. The server checks that certificate instead of (or in addition to) a password. This is stronger than password authentication because the private key never travels over the network. When configured, `pg_hba.conf` uses the `cert` authentication method on a `hostssl` line.

Comparison: In-Transit vs. At-Rest Encryption

The DP-300 exam covers Transparent Data Encryption (TDE) in Azure SQL Database. TDE encrypts data files, log files, and backups on disk automatically. It does not protect data as it moves between the server and a client — that is the job of TLS. Key distinction: TLS protects data on the wire; TDE protects data on the drive.

Safety and Operational Notes

Key terms9

SSL/TLSSecure Sockets Layer / Transport Layer Security — cryptographic protocols that create an encrypted tunnel for data moving between a client and a server over a network.
Certificate Authority (CA)An entity that issues and digitally signs certificates, allowing clients and servers to verify each other's identities.
server.keyThe PostgreSQL server's private key file used during the TLS handshake; must be kept secret, owned by the postgres OS user, and set to permission mode 0600.
hostsslA pg_hba.conf record type that allows only SSL-encrypted connections; connections attempted without encryption are refused.
sslmodeA libpq client connection parameter that controls whether and how the client requests SSL; values include disable, allow, prefer, require, verify-ca, and verify-full.
Encryption in transitProtection of data while it travels across a network connection, using protocols such as TLS; does not protect data stored on disk.
Transparent Data Encryption (TDE)A feature in Microsoft SQL Server and Azure SQL Database that automatically encrypts data files and backups on disk; protects data at rest rather than data in transit.
Self-signed certificateA certificate signed by its own private key rather than by a recognized external CA; suitable for testing and internal use but not trusted by default in most public clients.
Certificate-based client authenticationA PostgreSQL authentication method in which the client proves identity by presenting a certificate signed by a trusted CA, instead of or in addition to a password.
In class: Instructor-led demo: generate a self-signed CA and server certificate using OpenSSL; copy server.crt and server.key to the PostgreSQL data directory with correct file permissions (0600 owned by postgres); set ssl = on in postgresql.conf and reload; show psql connecting with sslmode=require. · Guided practice: learners replicate the demo on their own VM, then use the psql \\conninfo command and openssl s_client to confirm the TLS handshake succeeds; they also deliberately set sslmode=disable and confirm the connection is refused when pg_hba.conf requires hostssl. · Short research and discussion: learners read the DP-300 objective on Azure SQL transparent data encryption (TDE) and compare it to the in-transit encryption just configured — they write three bullet points distinguishing encryption in transit from encryption at rest.
Materials: OpenSSL command-line tool, PostgreSQL 16 SSL documentation, Microsoft DP-300 TDE reference material, Linux VMs, instructor demo slide deck
Check: Learner produces a terminal screenshot showing sslmode=require in \conninfo and a failed unencrypted connection attempt; instructor verifies outputs. Pass = both conditions demonstrated.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

PostgreSQL CE — Security and Authentication
1. Which file permission mode is required for the PostgreSQL server.key file, and why?
PostgreSQL CE — Security and Authentication
2. A pg_hba.conf line uses the record type 'hostssl'. What happens when a client attempts to connect with sslmode=disable?
PostgreSQL CE — Security and Authentication
3. After enabling SSL in postgresql.conf, which psql meta-command confirms that the active session is using TLS?
Microsoft DP-300 — Implement a Secure Environment
4. What is the primary distinction between encryption in transit and encryption at rest?
PostgreSQL CE — Security and Authentication
5. A DBA wants to verify the TLS handshake with a PostgreSQL server from the Linux command line without using psql. Which tool is most appropriate?
Microsoft DP-300 — Implement a Secure Environment
6. Microsoft Azure SQL Database Transparent Data Encryption (TDE) would protect against which of the following threats?
PostgreSQL CE — Security and Authentication
7. In a PostgreSQL certificate-based client authentication setup, what does the server verify during the TLS handshake?
PostgreSQL CE — Security and Authentication
8. A PostgreSQL server has ssl = on in postgresql.conf but pg_hba.conf uses 'host' (not 'hostssl') for all its entries. Which statement best describes the behavior?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.