PostgreSQL Error 42P12: invalid_database_definition
PostgreSQL error code 42P12 (invalid_database_definition) is raised when a CREATE DATABASE or ALTER DATABASE statement contains an invalid option, a non-existent object reference, or an incompatible combination of parameters. Unlike table-level definition errors, this error is scoped to the database object itself and must be resolved before any database can be successfully created or modified.
Top 3 Causes and Fixes
1. Non-Existent Tablespace
Specifying a tablespace that does not exist in pg_tablespace is the most common trigger for this error.
-- This will throw 42P12 if 'myspace' doesn't exist
CREATE DATABASE mydb TABLESPACE = myspace;
-- Check existing tablespaces first
SELECT spcname, pg_tablespace_location(oid) AS location
FROM pg_tablespace;
-- Create the tablespace first (ensure the OS directory exists)
-- Shell: mkdir -p /data/pgdata/myspace && chown postgres:postgres /data/pgdata/myspace
CREATE TABLESPACE myspace LOCATION '/data/pgdata/myspace';
-- Now safely create the database
CREATE DATABASE mydb
OWNER = myuser
TABLESPACE = myspace
ENCODING = 'UTF8'
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
TEMPLATE = template0;
2. Incompatible Encoding or Locale Combination
Mixing an encoding with a locale unsupported by your OS, or trying to use a different encoding than template1, will produce this error.
-- Problematic: may conflict with template1's encoding
-- CREATE DATABASE mydb ENCODING 'UTF8' LC_COLLATE 'ja_JP.UTF-8';
-- Correct: always use template0 when customizing encoding/locale
CREATE DATABASE mydb
ENCODING = 'UTF8'
LC_COLLATE = 'ja_JP.UTF-8'
LC_CTYPE = 'ja_JP.UTF-8'
TEMPLATE = template0;
-- Verify the database settings afterward
SELECT datname,
pg_encoding_to_char(encoding) AS encoding,
datcollate,
datctype
FROM pg_database
WHERE datname = 'mydb';
3. Invalid CONNECTION LIMIT Value or Bad GUC Parameter
Passing an illegal value to CONNECTION LIMIT (any negative number other than -1) or an unrecognized GUC parameter in a SET clause will raise 42P12.
-- Wrong: -5 is not a valid connection limit
-- ALTER DATABASE mydb CONNECTION LIMIT -5; -- 42P12 error
-- Correct: use -1 for unlimited, or a positive integer
ALTER DATABASE mydb CONNECTION LIMIT 200;
ALTER DATABASE mydb CONNECTION LIMIT -1; -- unlimited
-- Setting valid GUC parameters per database
ALTER DATABASE mydb SET work_mem = '64MB';
ALTER DATABASE mydb SET log_min_duration_statement = 2000;
-- Confirm applied settings
SELECT datname, setconfig
FROM pg_db_role_setting drs
JOIN pg_database d ON d.oid = drs.setdatabase
WHERE d.datname = 'mydb';
Quick Fix Checklist
-
Verify tablespace exists with
SELECT * FROM pg_tablespacebefore referencing it. -
Always use
TEMPLATE = template0when specifying a customENCODINGorLC_COLLATE. -
Use
-1or a positive integer forCONNECTION LIMIT; any other negative value is invalid. -
Validate GUC parameters with
SHOW ALLbefore using them inALTER DATABASE SET.
Prevention Tips
-
Pre-flight validation: Add a validation block before any
CREATE DATABASEcall to assert that all referenced objects (tablespaces, roles) exist and all parameter values are within acceptable ranges.
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_tablespace WHERE spcname = 'myspace'
) THEN
RAISE EXCEPTION 'Tablespace myspace does not exist. Create it first.';
END IF;
RAISE NOTICE 'Pre-flight checks passed.';
END;
$$;
-
Version-control your DDL scripts: Store all
CREATE DATABASEandALTER DATABASEstatements in Git and enforce peer code review. Automated linting tools or CI pipelines can catch typos, invalid locales, and missing dependencies before they reach production.
Related Errors
| Code | Name | Brief |
|---|---|---|
42P01 |
undefined_table |
Referenced object does not exist |
42710 |
duplicate_object |
Database or tablespace already exists |
42501 |
insufficient_privilege |
User lacks CREATEDB privilege |
53100 |
disk_full |
Tablespace directory has no space left |
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)