Database error codes, explained per engine
A searchable reference of 611 SQL Server, PostgreSQL, MySQL, SQLite and Oracle error codes, with causes, fixes and safe repro scripts.
Database error messages are written for the engine, not for you. SQL Server says
Msg 18456, Level 14, State 1 and stops there. PostgreSQL's
psql shows the message but hides the SQLSTATE unless you ask for it.
MySQL packs an error number and a SQLSTATE into one line. SQLite hands your driver an
integer, and Oracle gives you ORA- plus five digits. Then you search for
the code and end up reading a forum thread from 2011 where the accepted answer is
"restart the service".
So we built a reference at /errors. It covers SQL Server, PostgreSQL, MySQL, SQLite and Oracle, with one page per error that says what the error means, what usually causes it, and how to fix it, with SQL you can paste.
What is in it
Each engine has its own catalog, and the codes and message text come from that vendor's documentation: Microsoft's Database Engine events and errors reference, PostgreSQL's Appendix A, the MySQL 8.4 Error Message Reference, SQLite's result code page, and Oracle Database Error Help. Every page links back to its source. Here is what the catalogs hold today:
| Engine | Code type | Entries | Full write-ups |
|---|---|---|---|
| SQL Server | Error number | 94 | 94 |
| PostgreSQL | SQLSTATE | 262 | 63 |
| MySQL | Error number | 79 | 79 |
| SQLite | Result code | 105 | 40 |
| Oracle | Error code | 71 | 71 |
That's 611 entries, 347 of them with a page of their own. The gap is on purpose.
PostgreSQL's appendix lists every SQLSTATE, including codes like
00000 (successful completion) that nobody has ever searched for in a
panic. Those stay in the engine index as a code, a condition name and a group, so the
list is complete, but they don't get a thin page pretending to say something. A
request for one of them redirects to its row on the index.
The engine index groups entries the way the engine does. For PostgreSQL that's the
43 SQLSTATE classes (23 is integrity constraint violation,
40 is transaction rollback). SQL Server error numbers don't carry their
meaning, so they sit in 10 topic groups such as connection, transactions and
permissions. Each index opens with a short "how to read these codes" note: what
Level and State mean in a SQL Server message, the
bracketed SQLSTATE in a MySQL one, how SQLite packs extended codes into the high bits
(SQLITE_BUSY_SNAPSHOT is 517, which is 5 + 2 × 256).
What an entry contains
A full entry page has these sections, in this order:
- The official message text, with the vendor's placeholders left in, plus the severity for SQL Server and the SQLSTATE for MySQL.
- What it means, in plain words.
- Common causes, as a list of the specific situations that produce it.
- How to fix it, as numbered steps, many with a SQL snippet.
- Reproduce it safely: a short script that triggers the error on a scratch database. 98 entries have one so far.
- Where relevant, a reference table (the login-failure states for 18456, for example), a few FAQs, and a note on how SQLly helps.
- Related errors, which can cross engines, and more errors from the same group.
Walkthrough: SQL Server error 266
Error 266 is one of the most searched SQL Server errors, and the message doesn't help much:
Transaction count after EXECUTE indicates a mismatching number of BEGIN and
COMMIT statements. Previous count = %ld, current count = %ld.
The entry explains the mechanism. SQL Server tracks open transactions in
@@TRANCOUNT, and when a stored procedure, trigger or RPC call such as
sp_executesql returns, it compares the count with the value it had on
entry. If the two differ, you get 266. The causes it lists are the ones that actually
happen: an early RETURN after BEGIN TRANSACTION, a
ROLLBACK inside a transaction the caller opened (it always rolls back to
the outermost transaction and sets the count to 0), a driver sending
BEGIN TRANSACTION through sp_executesql, and
XACT_ABORT rolling everything back after an error in a nested procedure.
The repro is five lines:
-- Run on a scratch database.
CREATE OR ALTER PROCEDURE dbo.LeavesTranOpen AS BEGIN TRANSACTION;
GO
EXEC dbo.LeavesTranOpen; -- Msg 266: Previous count = 0, current count = 1
SELECT @@TRANCOUNT; -- 1: the transaction is still open
ROLLBACK;
DROP PROCEDURE dbo.LeavesTranOpen;
The first fix step is a full procedure template: check @@TRANCOUNT on
entry, open a transaction only if there isn't one, and use a savepoint otherwise, so
an error rolls back only the procedure's own work. The last step is the one people
miss. 266 often arrives as the second message, after a deadlock (1205) or a foreign
key violation (547) made the procedure roll back, so fix the first error first. The
FAQ also answers the question behind a lot of these searches: why the app gets 266
and SSMS doesn't. Drivers send parameterized statements through
sp_executesql, which SQL Server checks for a balanced count, while SSMS
sends a plain batch.
Walkthrough: PostgreSQL 40P01
40P01 is deadlock_detected. The
entry explains that PostgreSQL only looks for a lock cycle after a wait has lasted
deadlock_timeout (1 second by default), aborts one transaction
completely, and lets the other carry on. It lists the causes you'd expect, like two
transfers updating the same accounts in opposite order, and one that surprises people:
inserting child rows takes a FOR KEY SHARE lock on the parent row, which
can conflict with another transaction updating that parent.
The first fix is to retry, because a deadlock is a normal concurrency outcome and the
application should catch 40P01 and 40001 and run the whole
transaction again. The second is to take locks in a consistent order:
BEGIN;
SELECT * FROM accounts WHERE id IN (1, 2) ORDER BY id FOR UPDATE;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
The repro uses two sessions and a two-row table, with a note to run the steps in order in separate connections. Related errors include SQL Server 1205, MySQL 1213 and ORA-00060, because if you work on more than one engine, the same deadlock shows up under four codes.
That idea has its own table on the hub, "Same problem, different code". It lines up duplicate keys (2627, 23505, 1062, SQLITE_CONSTRAINT_UNIQUE, ORA-00001), foreign key and NOT NULL violations, missing tables and columns, deadlocks, rejected logins, values too long for a column, and lock timeouts, one column per engine. Where an engine has no single equivalent, the table says so instead of forcing a match.
How search works
The search box takes an error number, a SQLSTATE, a symbol like
ER_DUP_ENTRY, or a few words of the message. With JavaScript on, the page
fetches /errors/search-index.json once, the first time you focus the box,
and searches it in the browser as you type. What you type isn't sent anywhere. Without
JavaScript, the same box is a plain GET form, and the server runs the same ranking in
C#, so both paths return the same results.
The ranking is simple. Filler words like "error", "code" and "sqlstate" are dropped.
An exact match on a code, slug, symbolic name or alias scores highest. A single token
that starts a code comes next, so typing 184 lists the 184xx login errors.
Otherwise every word you typed has to start a word in the code, message or title.
"lock" matches "locked", but "locked" doesn't match "blocked". Entries with full
write-ups get a small bonus. If a search hits exactly one code, pressing Enter goes
straight to that page, so searching 18456 lands on
the 18456 page. On an engine index, the box only
searches that engine.
URLs are forgiving as well. Engine aliases and other casings redirect permanently to
the canonical address, so /errors/mssql/266 goes to
/errors/sql-server/266 and /errors/postgres/40p01 goes to
/errors/postgresql/40P01.
Where to go next
Start at /errors, or go straight to an engine index. SQLly
connects to all five of these engines from one app, and on SQL Server it shows the
error in full, as Msg 18456, Level 14, State 1, so you have the exact
number to look up. If the error turned out to be a timeout and you are now reading a
plan, reading query plans across
engines covers that part.