Postgres: FATAL: role does not exist
psql defaults to the OS user, which often has no role. Set PGUSER, create the role once per cluster and check Docker POSTGRES_USER.

Introduction & Quick Summary
A connection attempt fails before any SQL runs:
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed:
FATAL: role "root" does not existQuick answer: the client asked to authenticate as a role that this database cluster does not have. PostgreSQL roles are cluster-wide objects, not per-database, and the client picked the name from a default rather than from an explicit setting.
psql -U postgres -h localhost -d appdb
# or set it once
export PGUSER=postgres PGDATABASE=appdbThe default name is the operating-system user, which is why the error often names root, ubuntu or a CI runner's user. The fix is either to connect as an existing role or to create the missing one, and the second half is where most confusion starts: role "x" does not exist says nothing about passwords, pg_hba.conf or permissions. This guide covers the default, the Docker trap, clusters and ports, quoting, and managed services.
Core Mechanics, Signatures & Reproducible Setup
libpq resolves the connection user in this order: the user parameter in the connection string, PGUSER, then the OS user name. If that role is absent, authentication stops with this FATAL message.
whoami
# deploy
psql -d appdb
# FATAL: role "deploy" does not exist
psql -U postgres -d appdb -c '\du'
# List of roles with attributesA connection string has the same effect and is easier to get wrong:
postgresql://appuser:secret@db.internal:5432/appdb
| Source of the role name | Precedence | Typical mistake |
|---|---|---|
postgresql://user@host/db | highest | typo in the URL, stale secret |
-U flag | next | flag placed after -c and ignored |
PGUSER | next | exported in one shell, not in the service unit |
PGPASSWORD only | does not set the user | password provided, user missing |
| OS user | last | root, ubuntu, a container's numeric user |
.pgpass | supplies password only | file exists, role still absent |
| Check | Command | Result |
|---|---|---|
| Which cluster answers | psql -U postgres -c 'SHOW port' | port and version |
| Roles that exist | psql -U postgres -c '\du' | names and attributes |
| Can a role log in | \du shows Cannot login | NOLOGIN role |
| Databases | psql -U postgres -c '\l' | database list |
| Current user | psql -c 'select current_user' | the resolved role |
| Container env | docker inspect -f '{{.Config.Env}}' pg | POSTGRES_USER value |
Key Comparison Table
| Situation | What is missing | Fix |
|---|---|---|
Fresh install, psql as root | role for root | sudo -u postgres psql then create a role |
Docker with POSTGRES_USER=app | postgres role never created | connect as app, or recreate with the default |
App connects as admin | role not provisioned | create the role with a password |
| Restored dump to a new cluster | roles are not in the dump | recreate roles before restoring |
| Connecting to the wrong port | another cluster, other roles | check SHOW port |
| CI runner user | ephemeral OS user | set PGUSER in the job |
| Managed service | master user has a different name | use the console-provided user |
| Command | Creates | Login allowed | Use for |
|---|---|---|---|
CREATE ROLE app LOGIN PASSWORD '...' | role | yes | application user |
CREATE USER app PASSWORD '...' | alias, LOGIN implied | yes | same |
CREATE ROLE reader NOLOGIN | group role | no | grant container |
CREATE ROLE app SUPERUSER LOGIN | superuser | yes | never in production |
CREATE ROLE app LOGIN CREATEDB | role with createdb | yes | migration accounts |
ALTER ROLE app WITH LOGIN PASSWORD '...' | no new role | enables login | fixing NOLOGIN |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. The OS user default
sudo -u postgres psql
# or, on Debian and Ubuntu with peer authentication
sudo -u postgres createuser --interactive --pwpromptOn Debian-based systems the postgres OS user exists and peer authentication lets it connect without a password. A different OS user needs a matching role or an explicit -U. Creating a role for every developer account is fine for a local machine and wrong for production; use named application roles instead.
2. Creating the role properly
CREATE ROLE app_user LOGIN PASSWORD 'strong-secret';
CREATE DATABASE app_db OWNER app_user;
\c app_db
GRANT CONNECT ON DATABASE app_db TO app_user;
GRANT USAGE ON SCHEMA public TO app_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app_user;Roles are cluster-wide, so CREATE ROLE must run in the cluster, not in a specific database. Grants are per database. Since PostgreSQL 15, the public schema is no longer writable by every role by default, which is why a freshly created role can connect and still fail on CREATE TABLE or even SELECT.
3. The Docker POSTGRES_USER trap
docker run -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=app -d postgres:17With POSTGRES_USER=app, the entrypoint creates only app as the superuser; there is no postgres role, so psql -U postgres fails with exactly this error.
docker exec -it pg psql -U app -d appThe same applies to POSTGRES_DB, which changes the default database name. Existing volumes keep the original roles: changing the environment variable on an existing volume has no effect, and the fix is to connect with the original user.
4. Wrong cluster, wrong port
psql -U postgres -h /var/run/postgresql -c 'SHOW port'
psql -U postgres -h localhost -p 5433 -c 'SHOW port'Two installations on one host (for example PostgreSQL 16 from the distribution and 17 from a container) listen on different ports and sockets and have different role lists. A connection string that omits the port uses 5432, which may be neither. pg_lsclusters on Debian shows the mapping; psql -h /var/run/postgresql uses the socket instead of TCP and can reach a cluster that TCP settings hide.
5. Case, quoting and search paths
CREATE ROLE "Admin" LOGIN PASSWORD 'x'; -- quoted, case-sensitive
CREATE ROLE admin LOGIN PASSWORD 'x'; -- folded to lowercaseUnquoted identifiers fold to lowercase, so CREATE ROLE Admin creates admin. Connecting as Admin then fails with role "Admin" does not exist even though the role exists as admin. Always test with the exact string from \du. After connecting, SET ROLE other changes privileges, and search_path decides which schema unqualified names resolve to; neither affects the authentication role name.
6. Managed services and connection pools
psql "postgresql://postgres.abcdefgh:secret@aws-0-ap-northeast-1.pooler.supabase.com:6543/postgres"Managed providers supply the exact role name, which can include a project suffix, and a separate pooler host and port. Using the database user name from the dashboard with the direct host, or the pooler user with the direct port, produces this error or an authentication error. For RDS the master user is whatever was set at creation, not postgres. Restoring a logical dump into a fresh instance also requires creating roles first, since pg_dump does not include CREATE ROLE. Use the SQL formatter to prepare the grant statements and the SQL schema designer to keep the ownership model consistent.
Common Compilation/Runtime Mistakes & Version Gotchas
- Creating the role in the wrong database.
CREATE ROLEis cluster-wide; running it in one database does not create it twice, and running it in a container with a different data directory does nothing useful. - Expecting the dump to carry roles.
pg_dumpandpg_restoreinclude ownership by name but not role creation; restore into a cluster where the owner exists, or use--no-owner. - Confusing this error with authentication.
role does not existis emitted before any password check.password authentication failed for user "app"means the role exists and the password orpg_hba.confrule is the problem. - Editing
pg_hba.confto fix it. Host-based access rules decide how a role authenticates, not whether it exists. CREATE USERversusCREATE ROLE. Both create a role;CREATE USERimpliesLOGIN. A role created withoutLOGINconnects never and reports the same message for a different reason only after the password stage.- Version notes. PostgreSQL 15 changed the default privileges on the
publicschema and addedGRANTrequirements that surprise new deployments. PostgreSQL 17 changed the default of somepg_dumpfilters, but role handling is unchanged. - Superuser shortcuts in production.
ALTER ROLE app SUPERUSERfixes the error and removes every later permission error, which makes the real access model invisible.
The durable answer is a named role per application, created once per cluster, granted per database, and referenced explicitly in every connection string or service environment. Once that is in place, the error only appears when a new environment is provisioned without the role step.
Official documentation: database roles and createuser.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.