Sub topic 7
Authenticating clients
This chapter covers how PostgreSQL and MySQL decide whether a connection is allowed to log in. It assumes you can edit files as root, have run basic SQL, and have already set the listen address from the previous chapter. Roles and grants, which decide what a user may do after logging in, are covered in the next chapter.
Authentication versus authorization
Authentication proves who a client is. Authorization decides what that client may do. A database server does both, in that order, and a failure at either step produces a different error. Keeping the two apart makes troubleshooting much faster.
The two engines differ in what they check first:
| Question | PostgreSQL | MySQL |
|---|---|---|
| What identifies a client? | Role name, plus connection type, database and source address | Account name and host pattern together, for example `'appuser'@'10.0.0.%'` |
| Where are the rules? | `pg_hba.conf`, a file read top to bottom | The `mysql.user` table, with the most specific host match winning |
| How is a password checked? | Method named in the matching rule | Plugin named on the account |
PostgreSQL: pg_hba.conf
"HBA" stands for host-based authentication. Each line of pg_hba.conf has four fields (connection type, database, user, address) followed by a method and optional options. PostgreSQL scans the lines in order and applies the first line that matches the connection. It does not fall through to later lines. If the first match fails authentication, the client is refused, even when a later line would have accepted it.
Common methods
peeris for local Unix-socket connections only. The server asks the kernel which OS user is connecting and requires it to match the database role. No password is involved.scram-sha-256is the recommended password method. The password is never sent in a reusable form, and the server stores only a salted verifier.md5is an older password method. Keep it only for clients that cannot do SCRAM.certauthenticates with a client TLS certificate.rejectrefuses the connection immediately. It is useful as an explicit final rule.trustaccepts anyone who matches, with no password. Do not use it on a server that anyone but you can reach.
A restrictive example
# pg_hba.conf (TYPE DATABASE USER ADDRESS METHOD)
local all postgres peer
local appdb appuser scram-sha-256
hostssl appdb appuser 10.0.0.0/24 scram-sha-256
host all all 0.0.0.0/0 rejectRead it from the top. Administrators log in locally as the postgres OS user. The application can use the socket with a password, or connect from the private subnet over TLS. Everything else, including non-TLS host connections from the subnet, falls through to the reject line. A host line also matches TLS connections, so the order matters if you ever add one above the hostssl line.
Applying and checking changes
pg_hba.conf is reread on reload. You do not need a restart:
sudo systemctl reload postgresql
sudo -u postgres psql -c "SELECT line_number, type, database, user_name, address, auth_method, error FROM pg_hba_file_rules;"The pg_hba_file_rules view shows how the server parsed each line. A non-null error column means that line was ignored. Run this check after every edit. A typo can silently drop a rule, and a dropped rule changes which later rule matches.
Password storage
The password_encryption setting controls the format used when a password is set. It defaults to scram-sha-256 from PostgreSQL 14. A role whose password was stored as an MD5 hash years ago cannot log in through a scram-sha-256 rule, and the error looks like a wrong password. To find such roles and fix them:
-- Run as a superuser
SELECT rolname,
CASE WHEN rolpassword LIKE 'SCRAM-SHA-256$%' THEN 'scram'
WHEN rolpassword LIKE 'md5%' THEN 'md5'
ELSE 'none' END AS stored_as
FROM pg_authid
WHERE rolcanlogin;
-- Re-set the password so it is stored as SCRAM (use \password in psql to avoid typing it in plain text)
SET password_encryption = 'scram-sha-256';
ALTER ROLE appuser PASSWORD 'change-me';MySQL: accounts and plugins
MySQL has no host-based rule file. The account itself contains the host pattern, so 'appuser'@'10.0.0.%' and 'appuser'@'localhost' are two different accounts that can have different passwords and privileges. When several accounts match a login, MySQL picks the most specific host, so a literal address beats a pattern, and a pattern beats %. A client therefore sometimes authenticates as a different account than the one you expected.
Each account names an authentication plugin:
caching_sha2_passwordis the default in MySQL 8.0 and later. It needs either TLS or an RSA key exchange for the first login of a cached password.mysql_native_passwordis the older plugin. It is deprecated in 8.0 and disabled by default in 8.4, so only enable it for legacy clients you cannot update.auth_socketworks like PostgreSQLpeer. On Debian and Ubuntu packages,root@localhostoften uses it, which is whysudo mysqlworks without a password.
-- Inspect which plugin each application account uses
SELECT user, host, plugin FROM mysql.user WHERE user = 'appuser';
-- Change the plugin and password together
ALTER USER 'appuser'@'10.0.0.%'
IDENTIFIED WITH caching_sha2_password BY 'change-me';Changes to accounts made with CREATE USER and ALTER USER take effect immediately. You do not need FLUSH PRIVILEGES, which is only needed if you edit the grant tables directly.
Keeping passwords out of command lines and scripts
A password typed into a command line shows up in shell history and in the process list. Both engines have a client-side credentials file that avoids this. Both files must be readable only by their owner.
# PostgreSQL: host:port:database:user:password
printf '%s\n' '10.0.0.5:5432:appdb:appuser:change-me' > ~/.pgpass
chmod 600 ~/.pgpass
# MySQL: option file read by the mysql client
printf '%s\n' '[client]' 'user=appuser' 'password=change-me' > ~/.my.cnf
chmod 600 ~/.my.cnfPostgreSQL ignores a .pgpass file with looser permissions and prints a warning. The MySQL client also warns about group- or world-readable option files.
Typical errors
| Message | Likely cause |
|---|---|
| `no pg_hba.conf entry for host "10.0.0.9", user "appuser", database "appdb", SSL off` | No rule matches this address, user, database or TLS state. Check rule order and `hostssl` versus `host`. |
| `password authentication failed for user "appuser"` | Wrong password, or the stored hash type does not match the rule's method. |
| `Peer authentication failed for user "appuser"` | A `peer` rule matched, but the OS user name differs from the role name. |
| `ERROR 1045 (28000): Access denied for user 'appuser'@'10.0.0.9'` | Wrong password, or the client matched a different account than expected. |
| `Authentication plugin 'caching_sha2_password' cannot be loaded` | The client library is too old for the plugin. Update the client rather than weakening the account. |
A mismatch between what the client sends and what the server expects is the most common cause. Confirm what the server saw by reading its log, as covered in the troubleshooting chapter.
Exercise
A role appuser has a correct password, but connecting from 10.0.0.9 fails with "password authentication failed". pg_hba.conf has hostssl appdb appuser 10.0.0.0/24 scram-sha-256, and the role was created in 2019 on PostgreSQL 10. What is the most likely cause and how do you confirm and fix it?
Show answer
The role's password is probably stored as an MD5 hash, which a scram-sha-256 rule cannot verify. Confirm it by querying pgauthid and checking whether rolpassword starts with "md5" rather than "SCRAM-SHA-256$". Fix it by setting passwordencryption to scram-sha-256 and running ALTER ROLE appuser PASSWORD with the same or a new password. No reload is needed for the role change.