How to fix Permission denied for database in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 26, 2025· 187 views

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
  1. Verify the error and affected user
  2. Grant CONNECT privilege on the database
  3. Grant all database privileges (alternative)
  4. Grant schema and table privileges
postgresql — error
sudo -u postgres psql -d your_database
Permission 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

  1. User role was created without the CONNECT privilege (the default in recent PostgreSQL versions)
  2. Database privileges were restored from a backup without migrating user permissions
  3. User was intentionally restricted from connecting after being created
  4. A script or tool removed the CONNECT privilege from the user
  5. The user is trying to connect to a database they should not have access to

How to fix "Permission denied for database"

1Verify the error and affected user

Connect to PostgreSQL as the superuser (usually postgres) and identify which user is getting the error:

bash
sudo -u postgres psql -d your_database

Then try to reproduce the error using the affected user credentials to confirm the permission issue.

2Grant CONNECT privilege on the database

The most direct solution is to grant the CONNECT privilege to the user. In a psql session as the superuser, run:

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

3Grant all database privileges (alternative)

If the user needs full permissions on the database, grant all privileges instead:

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

4Grant schema and table privileges

If the user now connects but gets permission errors on schemas or tables, grant those privileges too:

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

5Verify the connection now works

Test the connection with the affected user to confirm the permission is fixed:

bash
psql -h localhost -U your_user -d your_database

If 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