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.
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.
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.
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;
ERROR: syntax error at end of input
Syntax error at end of input in PostgreSQL
Bind message supplies N parameters but prepared statement requires M
Bind message supplies N parameters but prepared statement requires M in PostgreSQL
Multidimensional arrays must have sub-arrays with matching dimensions
Multidimensional arrays must have sub-arrays with matching dimensions
ERROR: value too long for type character varying
Value too long for type character varying
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL