Fix PostgreSQL FATAL: role does not exist (psql, Docker)
PostgreSQL FATAL: role "root" does not exist means psql used your OS user name as the role. Pass -U with a real role; in Docker, check POSTGRES_USER.
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not existWithout -U, psql uses your current OS user name as the database role. Run it from a root shell or an account like ubuntu or ec2-user and the server rejects it because no role has that name. Pass an existing role such as -U postgres, or create a role with that name. In Docker, if you changed POSTGRES_USER, there is no postgres role at all.
[1] See which name you connected as. The name in quotes in the error is the role the server received. The run below is from the official Docker image, where socket connections use trust, so -U postgres alone got in. On a Debian or Ubuntu package install the socket uses peer, and -U postgres from root fails with Peer authentication failed; use sudo -u postgres psql instead (Fix PostgreSQL Peer authentication failed).
$ whoami; psqlrootpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist$ psql -U postgres -Atc 'SELECT current_user'postgresThe same naming rule applies over TCP (-h 127.0.0.1). If the connection hits a password-authentication line, the client shows password authentication failed instead — check the server log as in that guide.
[2] List the existing roles. Connect as an admin role and run \du.
$ psql -U postgres -c '\du' List of roles Role name | Attributes -----------+------------------------------------------------------------ appuser | postgres | Superuser, Create role, Create DB, Replication, Bypass RLS[3] Match the letter case. Unquoted names in SQL are folded to lower case, but the name you connect with is not. A role created with CREATE ROLE AppUser is really named appuser.
$ psql -U postgres -c 'CREATE ROLE AppUser LOGIN'CREATE ROLE$ psql -U AppUser -d postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "AppUser" does not exist$ psql -U appuser -d postgres -Atc 'SELECT current_user'appuserIf your application config has a mixed-case user name, lowercasing it is the easy fix. If you really need the capitals, create the role with double quotes, CREATE ROLE "AppUser" LOGIN, and always quote it in SQL.
[4] Create the role or change the default. To keep using your OS user name, create a role with the same name.
$ createuser -U postgres root && psql -Atc 'SELECT current_user'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "root" does not exist$ psql -d postgres -Atc 'SELECT current_user'rootThe role exists now, but the error became database "root" does not exist, because the default database name is the role name too — see Fix PostgreSQL database does not exist. If you'd rather not create a role, change only the default with an environment variable.
$ PGUSER=postgres psql -Atc 'SELECT current_user'postgresPut export PGUSER=postgres in your shell profile, or set the user in ~/.pg_service.conf or the connection string.
[5] Expect a different message when the role exists but can't log in. CREATE ROLE defaults to NOLOGIN; only CREATE USER defaults to LOGIN.
$ psql -U postgres -c 'CREATE ROLE reporter' && psql -U reporter -d postgresCREATE ROLEpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "reporter" is not permitted to log in$ psql -U postgres -c 'ALTER ROLE reporter LOGIN' && psql -U reporter -d postgres -Atc 'SELECT current_user'ALTER ROLEreporterIf it's a group role that only bundles privileges, don't grant LOGIN; create a separate login role and add it with GRANT reporter TO <role>.
[6] In Docker, check POSTGRES_USER. When set, the admin role gets that name and no postgres role is created.
$ docker exec db psql -U postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist$ docker exec db psql -U myapp -Atc 'SELECT current_user, current_database()'myapp|myappIf you changed the value and recreated the container but the new name still doesn't exist, the data volume survived and initialization was skipped. The image docs say these variables take effect only when the container starts with an empty data directory.
$ docker logs db 2>&1 | grep -i -E "skipping|initialization"PostgreSQL Database directory appears to contain a database; Skipping initialization$ docker exec db psql -U secondpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "second" does not exist$ docker exec db psql -U first -d first -Atc 'SELECT rolname FROM pg_roles WHERE rolcanlogin'firstConnect with the original name (first) and create the role: CREATE ROLE second LOGIN PASSWORD '<password>'. Deleting the volume and starting fresh also changes the name, but it deletes all data — only do that with a throwaway container.
Official references: psql — Connecting to a Database · CREATE ROLE · Identifier case folding (Lexical Structure) · libpq environment variable PGUSER · Docker official image postgres — environment variables