Incident Response

PostgreSQL "FATAL: role does not exist": Causes and Fixes

The server has no login role with exactly the name the client asked for. Here is how to see which name was requested, why it differs from what you expect, and how to keep roles in sync across restores, migrations and fleets of clusters.

· 14 min read · AG Data Team

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

  1. The OS user name was used by default. Connect explicitly: sudo -u postgres psql on a local socket with peer authentication, or psql -U postgres -d postgres. If you want your own account to work, create a role for it.
  2. There is no postgres role at all. The bootstrap superuser gets the name of the OS user who ran initdb unless -U was given (initdb). In our experience this is what happens on developer laptops where the installer runs initdb as your own account: the superuser is named after you, and tools that assume postgres fail. Connect as your own name and create postgres if you need it.
  3. Case folding. Unquoted identifiers are folded to lower case; quoted ones keep their case (identifiers). A role created as CREATE ROLE AppUser is stored as appuser, but the client sends AppUser verbatim. 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).
  4. Docker: POSTGRES_USER changed after the first start. The official image creates the user (default postgres) 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.
  5. 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.
  6. 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.

-- 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

PostgreSQL Errors Security Administration
Work with us

Roles and access out of sync after a migration?

We audit and rebuild PostgreSQL roles, grants and authentication for production clusters, including moves to managed services and multi-cluster fleets.

Get in touch More from the blog