Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#postgresql#roles#authentication#connection
Bold white headline reading NO SUCH ROLE beside a cyan Postgres cluster crate with an amber empty role slot.

Introduction & Quick Summary

A connection attempt fails before any SQL runs:

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

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

bash
psql -U postgres -h localhost -d appdb # or set it once export PGUSER=postgres PGDATABASE=appdb

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

bash
whoami # deploy psql -d appdb # FATAL: role "deploy" does not exist psql -U postgres -d appdb -c '\du' # List of roles with attributes

A connection string has the same effect and is easier to get wrong:

postgresql://appuser:secret@db.internal:5432/appdb
Source of the role namePrecedenceTypical mistake
postgresql://user@host/dbhighesttypo in the URL, stale secret
-U flagnextflag placed after -c and ignored
PGUSERnextexported in one shell, not in the service unit
PGPASSWORD onlydoes not set the userpassword provided, user missing
OS userlastroot, ubuntu, a container's numeric user
.pgpasssupplies password onlyfile exists, role still absent
CheckCommandResult
Which cluster answerspsql -U postgres -c 'SHOW port'port and version
Roles that existpsql -U postgres -c '\du'names and attributes
Can a role log in\du shows Cannot loginNOLOGIN role
Databasespsql -U postgres -c '\l'database list
Current userpsql -c 'select current_user'the resolved role
Container envdocker inspect -f '{{.Config.Env}}' pgPOSTGRES_USER value

Key Comparison Table

SituationWhat is missingFix
Fresh install, psql as rootrole for rootsudo -u postgres psql then create a role
Docker with POSTGRES_USER=apppostgres role never createdconnect as app, or recreate with the default
App connects as adminrole not provisionedcreate the role with a password
Restored dump to a new clusterroles are not in the dumprecreate roles before restoring
Connecting to the wrong portanother cluster, other rolescheck SHOW port
CI runner userephemeral OS userset PGUSER in the job
Managed servicemaster user has a different nameuse the console-provided user
CommandCreatesLogin allowedUse for
CREATE ROLE app LOGIN PASSWORD '...'roleyesapplication user
CREATE USER app PASSWORD '...'alias, LOGIN impliedyessame
CREATE ROLE reader NOLOGINgroup rolenogrant container
CREATE ROLE app SUPERUSER LOGINsuperuseryesnever in production
CREATE ROLE app LOGIN CREATEDBrole with createdbyesmigration accounts
ALTER ROLE app WITH LOGIN PASSWORD '...'no new roleenables loginfixing NOLOGIN

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. The OS user default

bash
sudo -u postgres psql # or, on Debian and Ubuntu with peer authentication sudo -u postgres createuser --interactive --pwprompt

On 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:17

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

bash
docker exec -it pg psql -U app -d app

The 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

bash
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

sql
CREATE ROLE "Admin" LOGIN PASSWORD 'x'; -- quoted, case-sensitive CREATE ROLE admin LOGIN PASSWORD 'x'; -- folded to lowercase

Unquoted 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

bash
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 ROLE is 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_dump and pg_restore include 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 exist is emitted before any password check. password authentication failed for user "app" means the role exists and the password or pg_hba.conf rule is the problem.
  • Editing pg_hba.conf to fix it. Host-based access rules decide how a role authenticates, not whether it exists.
  • CREATE USER versus CREATE ROLE. Both create a role; CREATE USER implies LOGIN. A role created without LOGIN connects 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 public schema and added GRANT requirements that surprise new deployments. PostgreSQL 17 changed the default of some pg_dump filters, but role handling is unchanged.
  • Superuser shortcuts in production. ALTER ROLE app SUPERUSER fixes 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.