Sub topic 6
Authenticating Clients with `auth_file` and `auth_query`
Prerequisites and authentication flow
Assume you know basic SQL, PostgreSQL roles, psql, and how to edit pgbouncer.ini. This chapter focuses on how PgBouncer verifies a client before allowing it into a connection pool.
A client sends a username and password to PgBouncer. PgBouncer checks the password against a stored PostgreSQL password hash. If the check succeeds, PgBouncer accepts the client connection. The PostgreSQL server connection is opened or reused separately.
A password hash is a one-way representation of a password. PgBouncer compares the client’s password with the stored hash. Store hashes rather than plaintext passwords whenever possible.
Using `auth_file`
auth_file points to a file containing usernames and password hashes. Each line uses this format:
"username" "password-hash"For SCRAM authentication, generate a correctly formatted line from PostgreSQL. Run this query as a superuser:
SELECT '"' || rolname || '" "' || rolpassword || '"'
FROM pg_authid
WHERE rolname = 'app_user';Copy the returned line into /etc/pgbouncer/userlist.txt. Restrict the file so that only the PgBouncer operating-system user can read it.
The related settings might look like this:
[pgbouncer]
auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txtauth_type tells PgBouncer which authentication protocol to require. The hash in the file must match the selected protocol and the PostgreSQL role’s password format.
| Method | Where PgBouncer finds the hash | Main advantage |
|---|---|---|
| `auth_file` | A local file | Simple and fast |
| `auth_query` | A PostgreSQL query | Centralized user management |
Using `auth_query`
auth_query lets PgBouncer ask PostgreSQL for a username and password hash when a client logs in. This avoids copying every role into a local file.
A dedicated login role should execute a narrowly scoped lookup function. Create the role and function in the database used for authentication:
CREATE ROLE pgbouncer_auth LOGIN PASSWORD 'change-me';
CREATE FUNCTION public.user_lookup(
IN i_username text,
OUT uname text,
OUT phash text
)
RETURNS record
LANGUAGE sql
SECURITY DEFINER
SET search_path = pg_catalog
AS $$
SELECT usename, passwd
FROM pg_shadow
WHERE usename = $1;
$$;
REVOKE ALL ON FUNCTION public.user_lookup(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION public.user_lookup(text) TO pgbouncer_auth;SECURITY DEFINER makes the function run with its owner’s privileges. The function owner must be trusted, because the function reads password hashes. Setting search_path reduces the risk that an object with the same name in another schema is called accidentally.
Configure PgBouncer with the role and query:
[pgbouncer]
auth_type = scram-sha-256
auth_user = pgbouncer_auth
auth_query = SELECT uname, phash FROM public.user_lookup($1)The authentication role needs access to the database where the query runs. Keep its permissions limited to this purpose, and use a strong password.
Choosing a method
Use auth_file for a small, stable deployment where simple local configuration is more important than centralized updates. Use auth_query when PostgreSQL roles change often or several PgBouncer instances must share the same source of truth.
After changing authentication settings, reload PgBouncer and test a real client login. A successful connection proves both password verification and database routing.
Exercise
Which method is usually better when many PgBouncer instances must recognize newly created PostgreSQL roles without manually copying files?
Show answer
auth_query, because each instance can retrieve the current password hash from PostgreSQL.