How to Read Your First Database Error Message

Database error messages can look rude and cryptic, especially the first few times. They are actually trying to help, and most of them fall into a handful of families. Once you can recognise the family, the fix is usually quick.

Joining two tables illustration

Step 1: read the whole message, slowly

Beginners often see "error" and stop reading. The useful part is usually in the middle: a table name, a column name, a line number or the word near the mistake. Copy the message into a notes file so you can look at it calmly.

Family 1: syntax errors

Looks like: near "FORM": syntax error (SQLite) or syntax error at or near "FORM" (PostgreSQL).

Means: the database could not understand the SQL. The quoted word is where it got confused; the actual mistake is often just before it.

Common causes: a typo such as FORM instead of FROM, a missing comma between columns, a comma after the last column, or mismatched quotes.

Family 2: missing things

Looks like: no such table: pet or relation "pet" does not exist; no such column: nmae.

Means: the name you used does not exist where the database is looking.

Common causes: a typo, singular versus plural (pet and pets), connecting to a different database file than you think, or the table never being created because a setup script failed.

A useful check in SQLite is .tables in the command-line tool; in PostgreSQL, \dt in psql.

Family 3: constraint failures

Looks like: NOT NULL constraint failed: pets.name; UNIQUE constraint failed: users.email; FOREIGN KEY constraint failed.

Means: the database refused data that breaks a rule you set. This is the database doing its job.

Common causes: a form field left empty, signing up twice with the same email, or inserting a row that points to a parent row that does not exist.

Family 4: wrong number of values

Looks like: Incorrect number of bindings supplied (Python sqlite3) or table pets has 3 columns but 2 values were supplied.

Means: the placeholders and the values you passed do not match.

Common cause in Python: passing one value without the trailing comma. (5) is just the number 5; (5,) is a tuple containing 5.

Family 5: connection errors

Looks like: connection refused, could not connect to server, password authentication failed, database "app" does not exist.

Means: the problem is reaching the database, not your SQL.

Common causes: the database server is not running, the host or port is wrong, the password is wrong, or the database name has a typo. Our quick answers on connection errors go through these one by one.

A quick reference

Message mentionsFirst thing to check
near, syntaxThe word just before the quoted one
no such, does not existSpelling, and which database you are connected to
constraint failedThe data you are inserting
bindings, values suppliedThe number of placeholders and values
connection, authenticationServer running, host, port, password

When you ask for help

Include the exact error text, the query and what you expected. Almost every helper will ask for those three things first. Our glossary explains words like constraint and binding if they are new.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Related pages on this site