How to fix Permission denied for database in PostgreSQL
This error occurs when a user lacks the required CONNECT privilege to access a PostgreSQL database. Grant the missing privileges using GRANT CONNECT or GRANT ALL PRIVILEGES to restore access.
Quick fix
Fixes Permission denied for database
sudo -u postgres psql -d your_database- Verify the error and affected user
- Grant CONNECT privilege on the database
- Grant all database privileges (alternative)
- Grant schema and table privileges
sudo -u postgres psql -d your_databasePermission denied for database
What this error means
The "Permission denied for database" error is thrown when PostgreSQL rejects a connection attempt because the user role lacks the required CONNECT privilege. PostgreSQL checks this privilege at connection startup before allowing access to the database. This is a fundamental access control mechanism that prevents unauthorized database connections. Unlike table-level permissions, database-level privileges control whether a user can establish a connection at all.
Typical symptoms
- psql: FATAL: permission denied for database connection refused
- Applications cannot connect to the database with the specified user credentials
- Error occurs immediately when attempting to connect, before executing any SQL queries
- Other users with proper privileges can access the same database without issues
Common causes
- User role was created without the CONNECT privilege (the default in recent PostgreSQL versions)
- Database privileges were restored from a backup without migrating user permissions
- User was intentionally restricted from connecting after being created
- A script or tool removed the CONNECT privilege from the user
- The user is trying to connect to a database they should not have access to
How to fix "Permission denied for database"
Connect to PostgreSQL as the superuser (usually postgres) and identify which user is getting the error:
sudo -u postgres psql -d your_databaseThen try to reproduce the error using the affected user credentials to confirm the permission issue.
The most direct solution is to grant the CONNECT privilege to the user. In a psql session as the superuser, run:
GRANT CONNECT ON DATABASE your_database TO your_user;Replace your_database with the actual database name and your_user with the PostgreSQL role that needs access.
If the user needs full permissions on the database, grant all privileges instead:
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_user;This grants CONNECT, CREATE (for schemas), and TEMPORARY (for temporary tables) privileges in a single command.
If the user now connects but gets permission errors on schemas or tables, grant those privileges too:
GRANT ALL PRIVILEGES ON SCHEMA public TO your_user;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO your_user;PostgreSQL 15+ requires explicit schema permissions by default.
Test the connection with the affected user to confirm the permission is fixed:
psql -h localhost -U your_user -d your_databaseIf successful, you should see the psql prompt. If it still fails, verify the role exists and check pg_hba.conf for connection restrictions.
Advanced notes
PostgreSQL versions 15 and later changed default privileges on the public schema—only the database owner can create objects in public by default, requiring explicit grants for other users. When restoring databases from pg_dump, use the -x flag to preserve privileges. If using connection poolers like PgBouncer, ensure the pooler user has CONNECT privilege. For read-only access, grant only SELECT on tables without the CREATE privilege: GRANT CONNECT ON DATABASE db TO user; GRANT USAGE ON SCHEMA public TO user; GRANT SELECT ON ALL TABLES IN SCHEMA public TO user;
Frequently asked questions
- What does "Permission denied for database" mean?
- The "Permission denied for database" error is thrown when PostgreSQL rejects a connection attempt because the user role lacks the required CONNECT privilege. PostgreSQL checks this privilege at connection startup before allowing access to the database. This is a fundamental access control mechanism that prevents unauthorized database connections.
- How do I fix "Permission denied for database"?
- This error occurs when a user lacks the required CONNECT privilege to access a PostgreSQL database. Grant the missing privileges using GRANT CONNECT or GRANT ALL PRIVILEGES to restore access.
- What causes "Permission denied for database"?
- User role was created without the CONNECT privilege (the default in recent PostgreSQL versions) Database privileges were restored from a backup without migrating user permissions User was intentionally restricted from connecting after being created
- Is the "Permission denied for database" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚PostgreSQL Official Documentation
- 📖PostgreSQL Official Docs - GRANT Statement(Official Docs)
- 📖PostgreSQL Official Docs - Privileges(Official Docs)