Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

psql says the role does not exist, even for postgres or your own user

Errorpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist

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

  • Applies to PostgreSQL 16, PostgreSQL 17, Ubuntu 24.04, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1The role in the message is your login name, and you typed plain psqlpsql used your OS user name as the role nameFix
    2. 2role "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. 3\du shows a role such as "Alice" but the client sends aliceThe role name differs in letter caseFix

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 exist
  • FATAL: 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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.