Sub topic 7
Authenticating Clients
Assume you are comfortable with Linux networking, systemd, package managers, and basic SQL. Authentication answers who may connect. Authorization answers what that connected identity may do. Configure both. A successful login should never imply unrestricted access.
The authentication flow
A database client supplies a destination, database name, username, and credentials. The server checks whether the connection is allowed, verifies the credentials, and then applies the permissions assigned to that identity.
The connection path has separate controls:
- The server must listen on an address and port.
- A firewall must allow the traffic.
- The database authentication rules must allow the client address and database.
- The client must prove its identity.
- The identity must have suitable privileges.
A connection can fail at any step. For example, “connection refused” usually indicates a listening or firewall problem. “Password authentication failed” usually means the server was reached, but the credentials or authentication method did not match.
PostgreSQL client authentication
PostgreSQL uses pg_hba.conf, the host-based authentication file. Each rule matches connection type, database, user, client address, and authentication method. PostgreSQL evaluates rules from top to bottom and uses the first match.
Prefer SCRAM password authentication for password-based connections. A rule using hostssl requires TLS, while host can allow an unencrypted connection unless other settings prevent it.
# pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
hostssl appdb appuser 10.0.0.0/24 scram-sha-256After changing this file, reload PostgreSQL so it rereads the rules:
sudo systemctl reload postgresqlA local administrative connection may use peer authentication. With peer, PostgreSQL checks the operating-system username instead of asking for a database password. This is useful for local administration, but it does not authenticate remote clients.
MySQL client authentication
MySQL accounts include both a username and a host pattern. Therefore, 'appuser'@'localhost' and 'appuser'@'10.0.0.%' are different accounts. The authentication plugin determines how the password is checked. Modern MySQL installations commonly use caching_sha2_password.
CREATE USER 'appuser'@'10.0.0.%'
IDENTIFIED WITH caching_sha2_password BY 'use-a-secret-manager';
GRANT SELECT, INSERT, UPDATE, DELETE
ON appdb.* TO 'appuser'@'10.0.0.%';Avoid wildcard host patterns broader than necessary. A user defined as 'appuser'@'%' may be usable from any reachable address unless other controls restrict the connection.
Test authentication safely
Test from the same network location as the application. Check both a valid login and a deliberately invalid login. Do not put production passwords in shell history. Use a protected client configuration file, an environment mechanism supplied by your deployment system, or a secret manager.
Authentication protects credentials in transit only when TLS is enabled. Configure TLS separately, then require it in the database authentication rules.
Exercise
A PostgreSQL client reaches the server, but receives “no pg_hba.conf entry.” What should you check first?
Show answer
Check the connection's database, username, client address, connection type, and port. Add a matching rule, place it before broader rules, and reload PostgreSQL.