0

I want a sql to get a list of CREATE of all Index and Constraints in a table 'tablename' just using SQL.

2

2 Answers 2

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.

0

Use the pg_get_indexdef and pg_get_constraintdef functions.

Not the answer you're looking for? Browse other questions tagged or ask your own question.