PostgreSQL Error 42622: Name Too Long Fix
In this article
PostgreSQL error 42622, "name too long," means an identifier you are trying to create exceeds the maximum length PostgreSQL allows. The fix is to shorten the identifier to 63 bytes or fewer, or let PostgreSQL truncate it automatically if that is acceptable. PostgreSQL stores identifiers in a fixed-size buffer, so any name longer than 63 bytes is rejected or silently truncated depending on the context. This guide walks you through identifying the offending name, shortening it safely, and avoiding the error in the future.
What You'll Need
- Access to a PostgreSQL client such as psql, pgAdmin, or another SQL tool
- The SQL statement or migration script that triggered the error
- Permission to create or alter the database objects involved
- A text editor to adjust long names in your code or migration files
Step-by-Step
- Read the full error message. PostgreSQL usually includes the offending name or at least the statement that failed. Note the exact identifier (table, column, index, constraint, function, or schema name) that is too long. If the message does not show it, look at the SQL statement you just ran and find the longest identifier.
- Count the bytes, not the characters. The limit is 63 bytes, not 63 characters. If your identifier contains non-ASCII characters (accented letters, emoji, CJK characters), each character may use more than one byte in UTF-8. For example, a name with 30 accented characters can exceed 63 bytes. Use a tool or query to check the byte length. In psql you can run
SELECT octet_length('your_identifier');to see the byte count. - Shorten the identifier. Rename the object to something 63 bytes or fewer. Good strategies include removing redundant prefixes or suffixes, using standard abbreviations (e.g.,
custfor customer,addrfor address), dropping version numbers or dates from the name, and keeping the most meaningful part of the name. For example,customer_shipping_address_validation_logbecomescust_ship_addr_val_log. - Update your SQL or migration file. Replace the long name everywhere it appears: in the CREATE statement, in foreign key references, in indexes, in application code, and in any ORM models or configuration files. A global search in your code editor helps catch all occurrences.
- Re-run the statement. Execute the corrected SQL. If the object was partially created before the error (for example, a table was created but a constraint failed), you may need to drop the partial object first. Use
DROP TABLE IF EXISTS name;or the appropriate DROP statement, then recreate. - Check for silent truncation. In some contexts PostgreSQL truncates identifiers longer than 63 bytes instead of raising an error. For example, if you create a table with a 70-byte name, PostgreSQL may create it with the first 63 bytes and issue a notice. Query
SELECT relname FROM pg_class WHERE relname LIKE 'your_prefix%';to see what was actually created. If you rely on the full name in application code, this mismatch will cause "relation does not exist" errors later. - Verify the change. After recreating, confirm the object exists with the intended name by querying the system catalogs, for example
SELECT * FROM information_schema.tables WHERE table_name = 'your_new_name';. Then test your application or queries against it. - Prevent recurrence. Add a naming convention to your project: keep identifiers short, avoid descriptive sentences, and document the 63-byte limit for your team. In migration scripts, add a check or lint step that flags identifiers over 63 bytes before they reach production.
Common Mistakes
- Counting characters instead of bytes. A 50-character name with multi-byte characters can exceed 63 bytes. Always check
octet_length()when non-ASCII characters are involved. - Only fixing the CREATE statement. The same long name may appear in foreign keys, indexes, triggers, or application code. If you miss one, the error reappears or you get a broken reference.
- Assuming truncation is harmless. PostgreSQL may truncate silently and create an object with a different name than you expect. Your application then fails with "relation does not exist" even though the CREATE succeeded.
- Forgetting quoted identifiers. If you use double quotes around an identifier, PostgreSQL preserves case and spaces, and the byte limit still applies. Quoted names that are too long fail the same way.
- Not cleaning up partial objects. A failed multi-statement transaction may leave objects behind if not wrapped in a transaction. Check for partial creations before retrying.
Tips
- Use a consistent abbreviation scheme across your schema so names stay short and predictable.
- Keep a shared list of approved abbreviations for common words (e.g.,
qty,desc,num) to avoid ad-hoc shortening. - When generating names programmatically (e.g., from user input or timestamps), hash or truncate the generated part to stay under the limit.
- Remember that the 63-byte limit applies to all identifiers: tables, columns, indexes, constraints, functions, schemas, and even cursor names.
- If you must keep a long human-readable description, store it in a comment on the object rather than in its name.
Troubleshooting
If the error persists after shortening the name, check the following. First, confirm you edited the correct statement; a migration tool may be running an older version of the file. Second, look for other identifiers in the same statement that exceed 63 bytes, such as a long index name or constraint name. Third, if you are using an ORM, check whether it auto-generates names (for example, foreign key constraint names) that combine table and column names and can exceed the limit; most ORMs let you override the generated name. Fourth, if the error occurs during a restore or import, the dump file may contain long names; edit the dump or restore into a schema with shorter names. Finally, if you see a notice about truncation instead of an error, query the system catalogs to find the actual stored name and update your code to match.
Frequently Asked Questions
What is the maximum identifier length in PostgreSQL?
PostgreSQL allows identifiers up to 63 bytes. Longer names are either rejected with error 42622 or truncated to 63 bytes, depending on the context.
Does the limit apply to quoted identifiers?
Yes. Double-quoted identifiers preserve case and special characters, but they are still subject to the 63-byte limit.
Why did my CREATE TABLE succeed but the table has a different name?
PostgreSQL may truncate an over-long identifier and issue a notice instead of an error. The table is created with the first 63 bytes of the name. Check the system catalogs to see the actual name.
Can I increase the identifier length limit?
No. The 63-byte limit is compiled into PostgreSQL and cannot be changed through configuration. You must shorten the identifier.
How do I check the byte length of a name?
Use SELECT octet_length('your_name'); in psql or any SQL client. This returns the number of bytes, which is what matters for the limit.
Does the error affect column names too?
Yes. The limit applies to all identifiers, including table names, column names, index names, constraint names, function names, and schema names.