LoginSignup
3
3

More than 5 years have passed since last update.

すべてのユーザテーブルのカラムについて、データ型とデフォルト値を調べるSQL

Last updated at Posted at 2016-06-02

すべてのユーザテーブルのカラムについて、データ型とデフォルト値を調べるSQLは、

SELECT
    n.nspname || '.' || c.relname AS table,
    a.attname AS column,
    format_type(a.atttypid, a.atttypmod) AS type,
    (SELECT
         pg_get_expr(d.adbin, d.adrelid)
     FROM
         pg_attrdef d
     WHERE
         d.adrelid = a.attrelid AND
         d.adnum = a.attnum AND
         a.atthasdef
    ) AS default
FROM
    pg_attribute a,
    pg_class c,
    pg_namespace n
WHERE
    a.attrelid = c.oid AND
    c.relnamespace = n.oid AND
    a.attnum > 0 AND
    NOT a.attisdropped AND
    c.relkind = 'r' AND
    n.nspname <> 'pg_catalog' AND
    n.nspname <> 'information_schema' AND
    n.nspname !~ '^pg_toast'
ORDER BY
    n.nspname,
    c.relname,
    a.attnum;
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3