Skip to content

Instantly share code, notes, and snippets.

@popravich
Last active November 17, 2024 09:04
Show Gist options
  • Save popravich/d6816ef1653329fb1745 to your computer and use it in GitHub Desktop.
Save popravich/d6816ef1653329fb1745 to your computer and use it in GitHub Desktop.
PostgreSQL index naming convention to remember

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;

Standard suffix for sequences is

seq for all sequences

Found here

@OsoianMarcel
Copy link

And also, why not to make them prefix (not suffix) for better readability?

@popravich
Copy link
Author

It was found here:
https://stackoverflow.com/questions/4107915/postgresql-default-constraint-names/4108266#4108266
Copied it to gist just in case (if I forget where to look for it)

@kozlovalex
Copy link

kozlovalex commented Jan 3, 2023

That's not so.

I created an index for the core_paymentdocument_2023_01_21 table and PG named it core_paymentdocument_2023_01__cashbox_number_currency_stat_idx.

I created an index for the core_paymentdocument_2023_01_22 table and PG named it core_paymentdocument_2023_01__cashbox_number_currency_stat_idx1.

Why did PG crop the table name?

@sher
Copy link

sher commented Jan 29, 2023

@kozlovalex index name length limit is 64 bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment