FATAL: role "…" does not exist in PostgreSQL means the server could not find a role with exactly the name the client sent at login. Nothing is broken in the database itself: either the client asked for a name you did not expect, or the role was never created on this particular cluster. The fix takes a minute once you know which of the two it is.
What the error actually means
Every connection is made as some role, and the role name is chosen by the client, not by the server (Database Roles). After authentication, the backend looks that exact name up in the role catalog. If nothing matches, it stops with FATAL: role "name" does not exist and SQLSTATE 28000, invalid authorization specification (miscinit.c). The comparison is byte-for-byte, so AppUser and appuser are different roles.
Roles are global to the cluster, not per database. If the role exists, it exists for every database on that server; whether it may connect to a given database is a separate question of privileges and pg_hba.conf (see our guide to "no pg_hba.conf entry for host").
$ psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed:
FATAL: role "ubuntu" does not exist
You will not always see this message. With scram-sha-256 or md5 authentication, PostgreSQL deliberately goes through a fake password exchange for unknown users so that the client cannot tell whether the role exists. The client gets "password authentication failed", and only the server log shows DETAIL: Role "…" does not exist. (crypt.c). So if you are chasing a password authentication failure, read the server log before resetting passwords. The plain "role does not exist" error usually shows up with peer, ident or trust.
-- what the client sees with scram-sha-256 / md5 in pg_hba.conf
FATAL: password authentication failed for user "app_user"
-- what the server log says
FATAL: password authentication failed for user "app_user"
DETAIL: Role "app_user" does not exist.
Step 1: find out which role was requested
The name in the error is the name the client sent. If you did not pass one, libpq uses the operating-system name of the user running the program, and the database name defaults to the user name (libpq connection parameters). That is why plain psql on a fresh Ubuntu server, logged in as ubuntu, fails with role "ubuntu" does not exist: the server only has postgres.
For applications, check the actual connection string the process uses, not the one in the wiki. Environment variables such as PGUSER, a ~/.pgpass or service file, an ORM default or a secret that was rotated in one place only can all change the name. If PgBouncer or another pooler sits in front, the role name it sends to PostgreSQL can differ from the one the application sends to the pooler, depending on its configuration.
Step 2: list the roles that do exist
-- connect as a role that does exist
sudo -u postgres psql
-- then
\du
SELECT rolname, rolcanlogin, rolsuper
FROM pg_roles
ORDER BY rolname;
If the role is listed but rolcanlogin is false, you would get a different error, role "…" is not permitted to log in. That role was created with CREATE ROLE without LOGIN (or as a group role) and needs ALTER ROLE … LOGIN, if logging in as it is really intended.
The usual causes and their fixes
- The OS user name was used by default. Connect explicitly:
sudo -u postgres psqlon a local socket with peer authentication, orpsql -U postgres -d postgres. If you want your own account to work, create a role for it. - There is no
postgresrole at all. The bootstrap superuser gets the name of the OS user who raninitdbunless-Uwas given (initdb). In our experience this is what happens on developer laptops where the installer runsinitdbas your own account: the superuser is named after you, and tools that assumepostgresfail. Connect as your own name and createpostgresif you need it. - Case folding. Unquoted identifiers are folded to lower case; quoted ones keep their case (identifiers). A role created as
CREATE ROLE AppUseris stored asappuser, but the client sendsAppUserverbatim. Stick to lower-case role names. If you rename a role to fix this, note that renaming clears an MD5-encrypted password, because MD5 uses the role name as salt (ALTER ROLE). - Docker:
POSTGRES_USERchanged after the first start. The official image creates the user (defaultpostgres) and a database with the same name, but these variables only take effect when the container starts with an empty data directory (postgres image docs). Changing the variable on an existing volume does nothing. Create the role by hand, or recreate the volume only if the data is disposable. - You are talking to a different server. In our experience a surprising number of these tickets are a local install on 5432 shadowing a container or tunnel, or a client resolving a stale DNS name. Run
SELECT inet_server_addr(), inet_server_port(), pg_postmaster_start_time(), version();from a session that works and compare. - The role was never created on this cluster. Typical after a restore or a move to a new server. Create it with the minimum it needs:
-- an application login role
CREATE ROLE app_user WITH LOGIN PASSWORD 'change-me';
GRANT CONNECT ON DATABASE appdb TO app_user;
-- or a role for your own OS account (local development)
sudo -u postgres createuser --interactive "$USER"
Restores: the same error, but as ERROR, not FATAL
pg_dump does not save roles; they are global objects that only pg_dumpall dumps (pg_dumpall). Restoring a single-database dump onto a new server therefore fails on every ALTER … OWNER TO and GRANT that names a missing role. Either copy the roles first, or restore with --no-owner and --no-privileges so that the connecting user owns everything and no grants are replayed (pg_restore).
# roles are not in a pg_dump archive: copy them first
pg_dumpall -h old-host -U postgres --roles-only > roles.sql
psql -h new-host -U postgres -f roles.sql
# on managed services without access to pg_authid
pg_dumpall -h old-host -U admin --roles-only --no-role-passwords > roles.sql
# or restore without ownership and GRANT statements
pg_restore -h new-host -U app_owner -d appdb --no-owner --no-privileges appdb.dump
--no-role-passwords reads from pg_roles instead of pg_authid, which helps on managed services where you are not a real superuser. Restored roles then have no password, and password authentication fails until you set one.
What changes on large systems and fleets
On one laptop this error is a typo. On dozens of clusters, it is usually a symptom of roles being managed by hand.
- Replication does not provision roles for you, except physical replication. A physical standby is a copy of the whole cluster, roles included. A logical replica, a blue-green target or a freshly restored server is not: logical replication does not carry schema or DDL (restrictions), and roles are not part of the data it ships. Create roles on the target before the cutover, not at the moment the first application connects.
- Treat roles as code. Keep role definitions and grants in migrations or configuration management, and apply them to every cluster. In our experience this avoids most "works on primary, fails on the new cluster" incidents after a migration.
- Passwords do not migrate on managed platforms. When
pg_authidis not readable, plan a password reset or a switch to IAM or certificate authentication as part of the migration, and test login for each service account before switching traffic. - Dropping old roles takes one pass per database. A role cannot be dropped while anything in any database references it, and
REASSIGN OWNEDandDROP OWNEDonly work in the current database, so you must run them in each one (Dropping Roles). On a cluster with hundreds of tenant databases, script it. - Alert on the rate, not the single event. A steady trickle of "role does not exist" in the logs is often scanners trying default names. A sudden spike right after a deploy usually means a configuration or secret points to a role that was not provisioned on that environment; in our experience that is the one worth paging on.
-- in EVERY database where the role owns objects
REASSIGN OWNED BY old_app TO new_app;
DROP OWNED BY old_app;
-- then, once, from any database
DROP ROLE old_app;
Key takeaways
- The error means no role has exactly the name the client sent; the name is chosen by the client.
- Without an explicit user, libpq uses the OS user name, and the database defaults to the same name.
- With password authentication the client sees "password authentication failed"; the server log says the role does not exist.
- Check
\duorpg_roles, watch case folding, and remember Docker variables only apply to an empty data directory. pg_dumpdoes not include roles: usepg_dumpall --roles-onlyor restore with--no-owner --no-privileges.- Across many clusters, manage roles as code and create them before any cutover.