151

When creating a table in PostgreSQL, default constraint names will assigned if not provided:

CREATE TABLE example (
    a integer,
    b integer,
    UNIQUE (a, b)
);

But using ALTER TABLE to add a constraint it seems a name is mandatory:

ALTER TABLE example ADD CONSTRAINT my_explicit_constraint_name UNIQUE (a, b);

This has caused some naming inconsistencies on projects I've worked on, and prompts the following questions:

  1. Is there a simple way to add a constraint to an extant table with the name it would have received if added during table creation?

  2. If not, should default names be avoided altogether to prevent inconsistencies?

1
  • 5
    I make it a rule to avoid default names for just this reason - you end up with the situation where in every deployment the constraints have different names. Commented Nov 5, 2010 at 16:39

2 Answers 2

420

The standard names for indexes in PostgreSQL are:

{tablename}_{columnname(s)}_{suffix}

where the suffix is one of the following:

  • pkey for a Primary Key constraint
  • key for a Unique constraint
  • excl for an Exclusion constraint
  • idx for any other kind of index
  • fkey for a Foreign key
  • check for a Check constraint
  • not_null for a Not Null constraint (since Postgres 18)

Standard suffix for sequences is

  • seq for all sequences

Proof of your UNIQUE-constraint:

NOTICE: CREATE TABLE / UNIQUE will create implicit index "example_a_b_key" for table "example"

Sign up to request clarification or add additional context in comments.

18 Comments

Very useful, thanks! Worth adding that foreign keys use the suffix fkey and that multi-column foreign key constraints only seem to include the first column name.
Exactly the answer I was looking for when googling "postgresql index naming conventions"
@someone ah sorry, I guess I meant the end of E.4.3.3 Utility Commands. It's the last bullet item before E.4.3.4 Data Types: "Use all key columns' names when selecting default constraint names for foreign keys (Peter Eisentraut)"
Using PostgreSQL version 11, I observed that the default name of a primary key constraint is always {tablename}_pkey, and does not include the name of the column or columns used in the primary key.
Using PostgreSQL version 11, I observed that the default name of a foreign key constraint is always {tablename}_{first columnname}_fkey,. i.e., regardless of how many columns are used in the foreign key, only the name of the first column is used..
|
54

The manual is pretty clear about this ("tableconstraint: This form adds a new constraint to a table using the same syntax as CREATE TABLE.")

So you can simply run:

ALTER TABLE example ADD UNIQUE (a, b);

1 Comment

Ah! I was mistakenly including CONSTRAINT like ALTER TABLE example ADD CONSTRAINT UNIQUE (a, b); and getting errors. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.