Postgres create table like exclude constraints but include indexes

Viewed 378

I am trying to recreate a table's structure using following statement. However I would like to exclude constraints but include indexes from the original table.

CREATE TABLE users_audit (like users EXCLUDING CONSTRAINTS);

Above statement works as expected as it create a new table users_audit without constraints. However I am not sure if it is possible to copy indexes using INCLUDING INDEXES or even include everything except primary key constraint. Looking at the documentation it seems like I can only pass EXCLUDING | INCLUDING.

1 Answers

You can say

CREATE TABLE users_audit (like users EXCLUDING CONSTRAINTS INCLUDING INDEXES);

That will create all indexes, so it also will create the indexes that implement primary key and unique constraints. However, foreign key constraints won't be created.

There is no way to only create the indexes that do not belong to a constraint. For that, you could say INCLUDING INDEXES and afterwards drop all constraints.

Related