This error occurs when PostgreSQL cannot find a specified role (user account) in the database. It commonly happens during initial setup when the default postgres role hasn't been created, or when referencing a role that was never created or was accidentally dropped.
PostgreSQL manages access control through roles, which are database entities that can own objects and have privileges. When you try to connect to the database or reference a role in a query, PostgreSQL looks up that role in its system catalog. If the role doesn't exist, you get this error. This is different from a password error—the role itself is missing, not just the credentials.
First, check what roles currently exist in your PostgreSQL installation. Connect as a superuser and list all roles:
sudo -u postgres psql -c "\\du"Or using SQL:
SELECT rolname FROM pg_roles ORDER BY rolname;This shows all roles and their attributes. If the postgres role is missing, you'll need to create it.
If the role doesn't exist, create it using the createuser utility (recommended) or SQL.
Using createuser (from your system terminal):
sudo -u postgres createuser -s postgresThe -s flag makes it a superuser. If you need a password:
sudo -u postgres createuser -s -P postgresUsing SQL (if already connected as a superuser):
CREATE ROLE postgres WITH SUPERUSER CREATEDB CREATEROLE LOGIN;Or with a password:
CREATE ROLE postgres WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'your_password';If you're using PostgreSQL in Docker, the postgres role should be created automatically. If it's missing:
1. Ensure you're not connecting to a local PostgreSQL installation instead of the container:
docker ps | grep postgres2. Check if the container is running and initialized:
docker logs <container_name>3. If the container postgres is correct but the role is missing, exec into it and create the role:
docker exec -u postgres <container_name> createuser -s postgresWhen moving a database between systems, roles don't migrate automatically. Before restoring a dump:
1. Create all required roles on the destination system first
2. Or, dump without role ownership information:
pg_dump --no-owner --no-acl source_db > dump.sql3. Then restore:
psql -U postgres -d target_db -f dump.sqlAlternatively, edit the dump file to replace role references with existing roles before restoring.
Once created, test that the role works:
# Using peer authentication (OS username matches role name)
sudo -u postgres psql
# Using password authentication (if password is set)
psql -U postgres -h localhost -WBoth should connect successfully without the 'role does not exist' error.
Case Sensitivity: PostgreSQL role names are case-insensitive by default. However, if you quote the role name when creating it (e.g., CREATE ROLE "MyRole"), it becomes case-sensitive. Always reference it with the exact same quoting.
Peer vs Password Authentication: PostgreSQL defaults to peer authentication on local connections, which requires the OS username to match the PostgreSQL role name exactly. Remote connections typically use password authentication. If your OS username is 'john' but you try to create only a 'postgres' role, peer auth will fail.
Superuser Roles: The postgres superuser role is special—it can create other roles and databases. If it's missing, you may need to reinitialize your PostgreSQL cluster using initdb with appropriate flags.
CREATEUSER Shorthand: createuser username is equivalent to CREATE ROLE username WITH LOGIN. Use createuser -s username to make a superuser.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL