PostgreSQL Database Errors
Every common PostgreSQL failure in one place: a unique-constraint collision on insert, a role that cannot read a table, a query that cannot find its relation, a deadlock between transactions, or a connection that will not open. Each guide opens with the direct cause, then the exact SQL and psql commands to confirm and fix it. Start with the fastest path below, or paste your own error into the assistant.
Fix your error now
Paste the error or logs and let the Incident Assistant diagnose it, or run a guided triage prompt with AI — no copy-paste.
Every guide in this cluster
50 guides-
Postgres 'duplicate key value violates unique constraint': Causes and Fix
Fix Postgres duplicate key value violates unique constraint — dedupe rows, upsert with ON CONFLICT, and reseed the serial sequence with setval.
-
Postgres 'relation does not exist': Fix search_path, Case, and Schema Issues
Fix the PostgreSQL 'relation does not exist' error: diagnose search_path issues, quoted-identifier case sensitivity, wrong database, schema, and permissions.
-
Postgres 'permission denied for table': Fix GRANTs, Schema USAGE, and Roles
Fix PostgreSQL 'permission denied for table': diagnose missing GRANTs, schema USAGE, role membership, and default privileges for new tables.
-
'deadlock detected' Transaction Lock Cycles
Fix the PostgreSQL 'deadlock detected' error: diagnose inconsistent lock ordering, long-running transactions, foreign key contention, and add retry logic.
-
'cannot drop ... because other objects depend on it' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'cannot drop because other objects depend on it': read the DETAIL, query pg_depend, drop dependents, or use CASCADE safely.
-
'could not serialize access due to concurrent update' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'could not serialize access due to concurrent update' (SQLSTATE 40001): retry on serialization failure, keep transactions short.
-
'current transaction is aborted, commands ignored until end of transaction block' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'current transaction is aborted, commands ignored until end of transaction block': find the first failed statement, ROLLBACK, use SAVEPOINT.
-
'database "..." does not exist' — Cause, Fix, and Troubleshooting Guide
Fix Postgres FATAL: database "appdb" does not exist — wrong dbname, dropped DB, wrong port or cluster, CREATE DATABASE, fix your DSN.
-
'division by zero' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'division by zero': guard denominators with NULLIF, CASE, or COALESCE, and filter zero rows for both / and % operators.
-
'insert or update on table violates foreign key constraint' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'insert or update on table violates foreign key constraint': the referenced parent row is missing. Insert the parent first or fix the key.
-
'invalid byte sequence for encoding' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'invalid byte sequence for encoding UTF8': client_encoding mismatch, NUL 0x00 bytes, iconv transcode, COPY ENCODING option.
-
'more than one row returned by a subquery used as an expression' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'more than one row returned by a subquery used as an expression': scalar subqueries, non-unique keys, LIMIT 1, IN, aggregates.
-
'must appear in the GROUP BY clause or be used in an aggregate function' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'must appear in the GROUP BY clause or be used in an aggregate function': group the column, aggregate it, or use DISTINCT ON.
-
'no pg_hba.conf entry for host' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'no pg_hba.conf entry for host': add a host/hostssl CIDR rule with scram-sha-256, open listen_addresses, then reload.
-
'operator does not exist' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'operator does not exist: integer = text': mismatched types, dropped implicit casts, enum/JSON operators. Add explicit casts to resolve.
-
'remaining connection slots are reserved' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'remaining connection slots are reserved for non-replication superuser connections': max_connections, reserved slots, pooling.
-
'syntax error at or near' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'syntax error at or near': read the LINE/caret pointer, add missing commas, quote reserved identifiers, fix driver placeholders.
-
'there is already a transaction in progress' — Cause, Fix, and Troubleshooting Guide
Understand Postgres WARNING there is already a transaction in progress — a nested BEGIN inside an open transaction, ORM conflicts, and fixes.
-
'type "..." does not exist' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'type "citext" does not exist': CREATE EXTENSION, search_path, schema-qualify the type, create enums before use.
-
'update or delete on table violates foreign key constraint' — Cause, Fix, and Troubleshooting Guide
Fix Postgres 'update or delete on table violates foreign key constraint': the parent row is still referenced by children. Repoint, cascade, or reorder.
-
'canceling statement due to conflict with recovery' Standby Query Conflicts
Fix PostgreSQL 'canceling statement due to conflict with recovery' on hot standbys: tune max_standby_delay, hot_standby_feedback, and long replica queries.
-
'canceling statement due to lock timeout' Lock Acquisition Failures
Fix PostgreSQL 'canceling statement due to lock timeout': diagnose blocking sessions, long transactions, and tune lock_timeout to avoid stuck queries.
-
'canceling statement due to statement timeout'
Fix PostgreSQL's statement timeout cancellations: diagnose slow plans, missing indexes, lock waits, bloat and stale statistics with EXPLAIN ANALYZE and pg_stat_statements.
-
'cannot execute INSERT in a read-only transaction' — Fix Read-Only Writes
Fix PostgreSQL 'cannot execute INSERT in a read-only transaction'. Diagnose a hot standby replica, default_transaction_read_only, or a mis-routed write.
-
'column "x" does not exist' — Fix Aliases, Quoting, and Search Path
Fix Postgres 'column does not exist': resolve case-folding and double-quote issues, wrong table aliases, missing search_path, GROUP BY/HAVING scope, and stale schema after migrations.
-
'Connection refused' Cannot Connect to Server
Fix PostgreSQL 'could not connect to server: Connection refused': diagnose a stopped server, listen_addresses binding, wrong port, firewalls, localhost-only sockets, and SELinux.
-
'could not resize shared memory segment' — Fix /dev/shm Exhaustion
Fix PostgreSQL 'could not resize shared memory segment' when parallel query DSM fails on a too-small /dev/shm. Diagnose with df, then size /dev/shm.
-
'could not serialize access due to concurrent update' Serialization Failures
Fix PostgreSQL 'could not serialize access due to concurrent update': understand REPEATABLE READ/SERIALIZABLE conflicts and add transaction retry logic.
-
'database is not accepting commands to avoid wraparound data loss'
Fix PostgreSQL's transaction ID wraparound shutdown: diagnose datfrozenxid age, stalled autovacuum, xmin-pinning transactions and replication slots, then recover safely.
-
'duplicate key value violates unique constraint' Unique Index Conflicts
Fix PostgreSQL 'duplicate key value violates unique constraint': diagnose race conditions, out-of-sync sequences, and resolve with UPSERT and ON CONFLICT.
-
'index row size exceeds btree maximum' — Fix Oversized B-tree Keys
Fix PostgreSQL 'index row size exceeds btree maximum 2704'. Learn why long text columns break B-tree limits, and use hash expression indexes or GIN instead.
-
'integer out of range' — Fix and Prevent
Fix 'integer out of range' in Postgres: handle sequence/id exhaustion, arithmetic overflow, and oversized literals by widening int to bigint safely, with real diagnostic SQL and migration steps.
-
'invalid input syntax for type integer' — Fix
Fix 'invalid input syntax for type integer' in Postgres: track down empty strings, non-numeric text, wrong column casts, and mismatched parameter types feeding an integer column, with real SQL.
-
'invalid page in block' Data Corruption Recovery
Fix PostgreSQL 'invalid page in block': diagnose page-level corruption, identify the affected relation, and recover with checksums and backups safely.
-
'No space left on device' Disk Full on Write
Fix the PostgreSQL 'No space left on device' error: diagnose a full data directory, WAL bloat, table bloat, temp files, and stuck replication slots.
-
'null value in column violates not-null constraint' — Fix
Fix 'null value in column violates not-null constraint' in Postgres: find the missing insert value, wrong default, failed trigger, or ALTER TABLE that hits legacy NULL rows, and prevent it.
-
'out of memory' — Diagnose and Fix
Fix Postgres 'out of memory' errors: trace work_mem blowups, high connection counts, huge hash/sort operations, and OOM killer kills, then right-size memory settings with real diagnostic SQL.
-
'out of shared memory' Lock Table Exhaustion
Fix PostgreSQL 'out of shared memory': diagnose max_locks_per_transaction exhaustion from many partitions and tables, and tune the lock table safely.
-
'password authentication failed for user' Login Fails
Fix PostgreSQL 'FATAL: password authentication failed for user': diagnose wrong passwords, pg_hba.conf md5/scram method mismatches, missing LOGIN roles, and encryption mismatches.
-
'prepared statement "S_1" already exists' — Fix Pooler Conflicts
Fix Postgres 'prepared statement already exists': the classic PgBouncer transaction-pooling clash with server-side prepared statements. Switch pool mode, disable prepares, or run PgBouncer 1.21+.
-
'remaining connection slots are reserved for non-replication superuser connections'
Fix PostgreSQL's connection slot exhaustion: diagnose max_connections, reserved slots, connection leaks, idle-in-transaction sessions and missing pooling with pg_stat_activity.
-
'role "name" does not exist' — Fix
Fix 'role does not exist' in Postgres: resolve missing login roles, wrong OS user mapping, dropped roles owning objects, and restore/failover gaps, with real psql diagnostics and safe fixes.
-
'SSL connection has been closed unexpectedly' TLS Disconnects
Fix PostgreSQL 'SSL connection has been closed unexpectedly': diagnose idle timeouts, server crashes, OOM kills, and network resets behind TLS disconnects.
-
'terminating connection due to administrator command' — Fix
Fix 'terminating connection due to administrator command' in Postgres: identify pg_terminate_backend calls, restarts, failovers, and idle-session reapers killing your session, with real diagnostics.
-
'the database system is starting up' — Fix
Fix 'the database system is starting up' in Postgres: understand crash recovery, WAL replay, standby startup, and slow restarts, with real diagnostics to tell a normal wait from a stuck server.
-
'there is no unique constraint matching given keys' — Fix Foreign Keys
Fix Postgres 'there is no unique constraint matching given keys for referenced table': add the missing UNIQUE/PRIMARY KEY on the parent, match column sets exactly, and handle partitioned tables.
-
'too many clients already' Connection Limit Exhausted
Fix PostgreSQL 'FATAL: sorry, too many clients already': diagnose exhausted max_connections, idle-in-transaction sessions, missing PgBouncer pooling, and connection leaks.
-
'too many connections for role' Per-Role Connection Limits
Fix PostgreSQL 'too many connections for role': diagnose CONNECTION LIMIT settings, leaked connections, and pooling to stay under per-role caps.
-
'value too long for type character varying(n)' — Fix
Fix 'value too long for type character varying(n)' in Postgres: find the oversized string, decide between widening the column or truncating input, and handle multibyte length gotchas, with real SQL.
-
'violates foreign key constraint' — Fix
Fix 'insert or update violates foreign key constraint' in Postgres: resolve missing parent rows, wrong insert order, bad delete/update cascade behavior, and orphaned data, with real diagnostic SQL.
Postgres AI prompts
Copy-paste, production-safe prompts for this stack.
More Postgres guides
Every Postgres prompt and troubleshooting guide.
Frequently asked questions
How do I fix "duplicate key value violates unique constraint" in Postgres?
INSERT ... ON CONFLICT DO UPDATE/NOTHING to upsert, or — if a serial/identity sequence fell behind after a manual insert — reseed it with SELECT setval(...). See duplicate key violates unique constraint.Why does Postgres say "relation does not exist" for a table that is there?
search_path, whether the identifier was created with quotes (making it case-sensitive), and that you are connected to the right database and schema. See relation does not exist.How do I resolve "permission denied for table" in PostgreSQL?
USAGE on the schema, check role membership, and set ALTER DEFAULT PRIVILEGES so future tables inherit access. See permission denied for table.