I want a sql to get a list of CREATE of all Index and Constraints in a table 'tablename' just using SQL.
2 Answers
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.