PostgreSQL Error 42939: Reserved Name Fix
In this article
PostgreSQL error 42939 means you tried to create or rename a database object using a name that PostgreSQL reserves for system use. The fix is to choose a different name, or—if you are migrating an existing schema—rename the object and update every reference to it. You cannot disable this check or whitelist a name; the reserved-word list is compiled into the server. The error appears when you run CREATE ROLE, CREATE DATABASE, CREATE SCHEMA, or ALTER ... RENAME TO with a name that matches a reserved keyword or a system-reserved prefix. The exact wording is usually ERROR: 42939: name "..." is reserved or ERROR: unacceptable name: "..." depending on your PostgreSQL version and client. The object is not created, and no partial state is left behind, so you can retry safely after renaming.
What You'll Need
- Access to a PostgreSQL client (psql, pgAdmin, DBeaver, or your application's migration tool) with a role that has
CREATEROLE,CREATEDB, or ownership of the object you are renaming. - The exact name that triggered the error, copied from the error message.
- A list of every place that name is referenced: application config, connection strings, SQL scripts, ORM models, and backup jobs.
- Permission to run
ALTER ... RENAME TOon the affected object, or to update and redeploy application code.
Step-by-Step
- Read the full error message. In psql, the error includes a
DETAILline naming the reserved word or prefix. For example,DETAIL: "user" is a reserved word.Copy that exact token; it tells you which rule you hit. - Check the name against PostgreSQL's reserved words. The official documentation lists reserved and non-reserved keywords for each major version. Names like
user,order,group,table,select,limit,offset,check,default,primary,references,unique,constraint,current_user,session_user, andlocaltimeare commonly reserved. System prefixes such aspg_are also blocked for user-created objects. - Choose a replacement name. Append a suffix or use a domain-specific prefix:
app_userinstead ofuser,ordersinstead oforder,customer_groupinstead ofgroup. Keep names lowercase and use underscores to avoid quoting rules later. - If the object already exists, rename it. For a table:
ALTER TABLE old_name RENAME TO new_name;. For a role:ALTER ROLE old_name RENAME TO new_name;. For a database:ALTER DATABASE old_name RENAME TO new_name;. For a schema:ALTER SCHEMA old_name RENAME TO new_name;. Run each statement in a transaction if your client supports it, and verify with\dt,\du, or\lin psql. - Update every reference. Search your codebase, configuration files, and connection strings for the old name. In application config, change the database user, schema, or table name. In ORM models, update the table name mapping. In migration scripts, edit the
CREATEstatement so future runs use the new name. Re-run any migration that failed so the object is created with the corrected name. - Re-test the original command. Run the same
CREATEorALTERstatement with the new name. A successful run returnsCREATE ROLE,CREATE DATABASE, orALTER TABLEwith no error. If you still see 42939, the replacement name also collides—return to step 2. - Verify dependent objects. Check views, foreign keys, indexes, and grants that referenced the old name. PostgreSQL updates internal dependencies automatically when you rename an object, but external references (application queries, stored procedures in other databases, cron jobs) do not update. Test a read and a write through the application to confirm nothing broke.
Common Mistakes
- Quoting the reserved word instead of renaming. Wrapping a reserved word in double quotes (
"user") makes PostgreSQL accept it as an identifier in many contexts, but it does not clear error 42939 for roles, databases, and schemas. Quoted identifiers also force you to quote the name in every future query, which breaks tools that do not quote consistently. - Renaming only the database and forgetting the role. A connection string has both a user and a database name. If the error came from the role, renaming the database leaves the login failure in place.
- Editing the migration file but not the live database. If the object was already created under a different name, changing the migration script does not rename the existing object. Run the
ALTER ... RENAME TOstatement against the live database as well. - Using a
pg_prefix for a user table. PostgreSQL reserves thepg_prefix for system catalogs. Even if a name is not a keyword, apg_prefix triggers the same class of error. - Assuming the error is version-specific. The reserved-word list grows between major versions. A name that worked on an older server can fail after an upgrade. Check the keyword list for the version you are actually running.
Tips
- Prefix application objects with a short project or schema code, such as
shop_ordersorcrm_user, to avoid collisions with both reserved words and future system names. - Run
SELECT word FROM pg_get_keywords() WHERE catcode = 'R';to list reserved words for your exact server version. This is faster than guessing from documentation. - Keep identifiers lowercase. PostgreSQL folds unquoted names to lowercase, so
Useranduserare the same identifier and both hit the reserved-word check. - When migrating from another database, run a pre-flight check that compares every table, column, role, and schema name against
pg_get_keywords()before importing. - Use a naming convention document in your repository so new tables and roles are reviewed against it before they reach production.
Troubleshooting
If the error persists after renaming, confirm you are connected to the intended database and running as the intended role. A common cause is a stale connection pool still using the old role name; restart the application or recycle the pool. If ALTER ... RENAME TO itself fails with 42939, the new name is also reserved—pick another. If you cannot rename a role because it owns objects, reassign ownership first with REASSIGN OWNED BY old_role TO new_role; and then rename. If a migration tool reports 42939 but manual SQL succeeds, the tool may be caching an old schema snapshot; clear its cache or restart the tool. If the error appears during a restore from a dump file, edit the dump's CREATE statements to use safe names before restoring, or restore into a fresh database and rename afterward. Never edit system catalogs directly to bypass the check; that can corrupt the cluster and is not supported.
Frequently Asked Questions
Can I disable the reserved-name check?
No. The check is part of the server's parser and identifier validation. The only supported fix is to use a different name.
Does quoting a reserved word fix error 42939?
Quoting can make some SQL statements accept a reserved word as an identifier, but it does not reliably clear 42939 for roles, databases, and schemas, and it creates maintenance problems because every reference must be quoted identically.
Which names are reserved?
PostgreSQL reserves SQL keywords such as user, order, group, select, table, and limit, plus any name beginning with pg_. The exact list depends on your server version; query pg_get_keywords() to see it.
Will renaming a table break my application?
PostgreSQL updates internal dependencies such as views and foreign keys automatically, but application queries, ORM mappings, and external scripts are not updated. Search for the old name and update every reference before deploying.
Why does the error appear only on the production server?
Production may run a newer PostgreSQL major version with a larger reserved-word list, or the object may already exist there under a conflicting name. Compare the server version and the existing object names between environments.
Can I use a reserved word as a column name?
Some reserved words are accepted as column names when quoted, but the same maintenance problems apply. Use a non-reserved synonym such as login_name instead of user to keep queries portable and readable.