psql says the role does not exist, even for postgres or your own user
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not existShort answer
The server found no role with the name the client sent. psql sends your operating-system user name unless you pass -U, so the name is often not the one you meant. List the roles from a session that works, then connect with -U or create the missing role.
Fix it fast
1Find out which cause you have
Look at the name in the error, then ask the server which roles exist. If you cannot log in at all, use whichever superuser the install created (see the first cause).
psql -d postgres -c "\du"2Match what you see, then jump to its fix
- 1
The role in the message is your login name, and you typed plain psqlpsql used your OS user name as the role nameFix - 2
role "postgres" does not exist on a fresh install, usually a package that runs under your own accountThe installer created a superuser named after the OS user, not postgresFix - 3
\du shows a role such as "Alice" but the client sends aliceThe role name differs in letter caseFix
- 1
What does the error look like?
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not existFATAL: role "username" does not exist
Cause 1 of 3
psql used your OS user name as the role name
1Check
psql defaults the database user name to the operating-system user name, and the database name to that same value. A role of that name only exists if somebody created it.
whoami
psql -U postgres -d postgres -c "\du"2Fix
Name the role explicitly with -U, and name the database with -d. Or create a role for your OS user once, from an account that can log in.
psql -U postgres -d postgres
# or, once, create a login role named after your OS user
createuser --superuser -U postgres "$(whoami)"3Verify
Plain psql, with no flags, now reaches a prompt (the database named after you must exist too: createdb).
psql -d postgres -c "\conninfo"Confirmed by postgresql.org
Cause 2 of 3
The installer created a superuser named after the OS user, not postgres
1Check
initdb makes one superuser and names it after the OS user that ran initdb; postgres is only common, not guaranteed. List the roles using your own OS name.
psql -U "$(whoami)" -d postgres -c "\du"2Fix
Use the role that exists. If an application or tutorial expects postgres, create it as a superuser from that account.
createuser --superuser --pwprompt "postgres"3Verify
Connect as postgres.
psql -U postgres -d postgres -c "select current_user"Confirmed by postgresql.org
Cause 3 of 3
The role name differs in letter case
1Check
Role names follow identifier rules: unquoted names fold to lower case, double-quoted names keep their case. A role made as "Alice" is not found by alice.
psql -d postgres -c "select rolname from pg_roles where lower(rolname) = lower('Alice')"2Fix
Send the exact name in -U, or create the lower-case role you want.
psql -U "Alice" -d postgres
# or
psql -d postgres -c "CREATE ROLE alice LOGIN"3Verify
Connect with the name your application uses.
psql -U alice -d postgres -c "select 1"Confirmed by postgresql.org
How do I stop it coming back?
- Put the user name in the connection string or PGUSER instead of relying on the OS default.
- Create one role per application, with LOGIN, rather than sharing the superuser.
- Write role names in lower case so quoting never matters.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
Peer authentication failed for user, on a local PostgreSQL connection
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: Peer authentication failed for user "postgres"
A pg_hba.conf line with method peer only lets a local socket client in when its OS user name equals the requested database user.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
IntelliJ IDEA Test Connection fails for a PostgreSQL data source
Connection to localhost:5432 refused.
When Test Connection fails, work from the bottom up.
4 causes, each with a fix