I want a sql to get a list of CREATE of all Index and Constraints in a table 'tablename' just using SQL.
Asked
Active
Viewed 595 times
0
-
2https://stackoverflow.com/questions/25958693 – Feb 18 '20 at 19:15
-
1https://dba.stackexchange.com/questions/214863/how-to-list-all-constraints-of-a-table-in-PostgreSQL – CR241 Feb 19 '20 at 00:46
2 Answers
0
To show all the indexes of a table, use pg_indexes PostgreSQL List Indexes
SELECT
indexname,
indexdef
FROM
pg_indexes
WHERE
tablename = 'table_name';
Replace <table name> with the name of your table.
Constraints can be retrieved via pg_catalog.pg_constraint. pg_constraint
SELECT con.*
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = '<schema name>'
AND rel.relname = '<table name>';
Replace <schema name> with the name of your schema and <table name> with the name of your table.
CR241
- 1,485
- 3
- 17
- 32