What the message looks like
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "deploy" does not existCommon causes
psqlor the app connects without a user name, so the OS user name is used (for exampledeployorwww-data), and no role with that name was created.- Typo or wrong case. Role names are case-sensitive when they were created in quotes (
"App"is notapp). - Wrong cluster or server. Debian and Ubuntu can run several clusters on different ports (5432, 5433), and the role exists in another one, or in another container.
- In Docker,
POSTGRES_USERonly takes effect when the data directory is initialized for the first time. With an existing volume the old roles stay. - After a restore from
pg_dumpthe roles are missing, because a dump of one database does not contain roles.
Check
- Existing roles in the cluster
sudo -u postgres psql -c '\du' - Roles and whether they may log in
sudo -u postgres psql -Atc "SELECT rolname, rolcanlogin FROM pg_roles ORDER BY 1" - Which OS user and which PGUSER does the client use?
id -un; echo "PGUSER=$PGUSER" - Which clusters and ports run on this host? (Debian/Ubuntu)
pg_lsclusters
Fix
- Connect as the administrator and look at the existing roles:
sudo -u postgres psql, then\du. - Create the missing role:
CREATE ROLE deploy LOGIN PASSWORD 'strong-password';, or from the shellsudo -u postgres createuser --pwprompt deploy. - Give the role a database to work in:
CREATE DATABASE appdb OWNER deploy;. Without-dpsqlalso tries a database with the same name as the user, which leads toFATAL: database "deploy" does not existnext. - Name the user explicitly in the connection:
psql -U app -d appdb -h localhost, or setPGUSER, or put the user in the connection string of the app. - Check which cluster you reach:
pg_lsclustersshows ports, and-p 5433selects another one. In Docker verify the user withdocker execand\du. - After a restore, bring the roles along:
pg_dumpall --roles-onlyon the source and import the result on the target before restoring the data.
Typical scenario
An admin sets up a new Ubuntu server, installs postgresql and logs in as the normal user deploy. Running psql stops at once with FATAL: role "deploy" does not exist. The reason: only the role postgres exists. sudo -u postgres createuser --pwprompt app and createdb -O app appdb create a dedicated user and database for the application.
FAQ
Why does it work with sudo -u postgres psql?
That command runs psql as the OS user postgres, so the role postgres is used, and it exists on every cluster. The error appears as soon as another OS user starts psql without -U.
Is a role the same as a user?
Since PostgreSQL 8.1 users and groups are both roles. A role with the LOGIN attribute is a user. CREATE USER is just CREATE ROLE ... LOGIN, so the error message always says role.
How do I change the role name if it was spelled wrong?
With ALTER ROLE oldname RENAME TO newname;. A password that was stored as MD5 is cleared by this, because the name was part of the hash. Set the password again afterwards.
The role exists but the error still appears. What could it be?
Most likely the connection landed on another server or cluster. In a working session run SELECT inet_server_port(), current_user; to see where you connect, and compare host and port with the app.
How do I create a read-only user?
Create the role with CREATE ROLE reader LOGIN PASSWORD '...' and then grant CONNECT on the database and SELECT on the tables. A new role has no table privileges by default.