Get the default values of table columns in Postgres?

前端 未结 6 2009
温柔的废话
温柔的废话 2020-11-29 11:21

I\'m looking for a way to run a query to find the default values of the columns of a table in Postgres. For example, if I made a table with the following query:

6条回答
  •  孤城傲影
    2020-11-29 11:48

    Use the information schema:

    SELECT column_name, column_default
    FROM information_schema.columns
    WHERE (table_schema, table_name) = ('public', 'mytable')
    ORDER BY ordinal_position;
    
     column_name │             column_default             
    ─────────────┼────────────────────────────────────────
     integer     │ 2
     text        │ 'I am default'::character varying
     moretext    │ 'I am also default'::character varying
     unimportant │ 
    (4 rows)
    

    Up to the schema naming, this should work in any SQL database system.

提交回复
热议问题