0
0
PostgresqlDebug / FixBeginner · 3 min read

How to Fix 'Role Does Not Exist' Error in PostgreSQL

The role does not exist error in PostgreSQL happens when you try to connect or assign permissions to a user role that is not created yet. To fix it, create the missing role using CREATE ROLE or correct the role name in your commands or connection settings.
🔍

Why This Happens

This error occurs because PostgreSQL cannot find a user or role with the name you specified. Roles are like user accounts in PostgreSQL, and if you try to connect or grant permissions to a role that does not exist, PostgreSQL will show this error.

sql
psql -U missing_role -d mydb

-- or

GRANT ALL PRIVILEGES ON DATABASE mydb TO missing_role;
Output
psql: FATAL: role "missing_role" does not exist ERROR: role "missing_role" does not exist
🔧

The Fix

You need to create the missing role before using it. Use CREATE ROLE or CREATE USER (which is a role with login permission). Also, double-check the role name for typos.

sql
CREATE ROLE missing_role LOGIN PASSWORD 'your_password';

-- Then you can connect or grant privileges
GRANT ALL PRIVILEGES ON DATABASE mydb TO missing_role;
Output
CREATE ROLE GRANT
🛡️

Prevention

Always verify that the role exists before using it. Use \du in psql to list roles. When scripting, create roles explicitly before assigning permissions or connecting. Avoid typos by copying role names from the role list.

sql
\du
Output
List of roles Role name | Attributes | Member of -----------+------------+----------- missing_role | Login | {}
⚠️

Related Errors

Other common errors include:

  • FATAL: password authentication failed for user — means the password is wrong or user cannot login.
  • permission denied for database — means the role exists but lacks rights.

Fix these by checking passwords and granting correct privileges.

Key Takeaways

The 'role does not exist' error means PostgreSQL can't find the user role you specified.
Fix it by creating the missing role with CREATE ROLE or CREATE USER before use.
Check role names carefully to avoid typos causing this error.
Use \du in psql to list existing roles and verify before connecting or granting rights.
Related errors often involve authentication or permission issues, fix by checking passwords and privileges.