How to generate the “create table” sql statement for an existing table in postgreSQL

后端 未结 19 2003
花落未央
花落未央 2020-11-28 17:49

I have created a table in postgreSQL. I want to look at the SQL statement used to create the table but cannot figure it out.

How do I get the create table

相关标签:
19条回答
  • 2020-11-28 18:00

    Even more modification based on response from @vkkeeper. Added possibility to query table from the specific schema.

    CREATE OR REPLACE FUNCTION public.describe_table(p_schema_name character varying, p_table_name character varying)
      RETURNS SETOF text AS
    $BODY$
    DECLARE
        v_table_ddl   text;
        column_record record;
        table_rec record;
        constraint_rec record;
        firstrec boolean;
    BEGIN
        FOR table_rec IN
            SELECT c.relname, c.oid FROM pg_catalog.pg_class c
                LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
                    WHERE relkind = 'r'
                    AND n.nspname = p_schema_name
                    AND relname~ ('^('||p_table_name||')$')
              ORDER BY c.relname
        LOOP
            FOR column_record IN
                SELECT
                    b.nspname as schema_name,
                    b.relname as table_name,
                    a.attname as column_name,
                    pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
                    CASE WHEN
                        (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
                         FROM pg_catalog.pg_attrdef d
                         WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
                        'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
                                      FROM pg_catalog.pg_attrdef d
                                      WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
                    ELSE
                        ''
                    END as column_default_value,
                    CASE WHEN a.attnotnull = true THEN
                        'NOT NULL'
                    ELSE
                        'NULL'
                    END as column_not_null,
                    a.attnum as attnum,
                    e.max_attnum as max_attnum
                FROM
                    pg_catalog.pg_attribute a
                    INNER JOIN
                     (SELECT c.oid,
                        n.nspname,
                        c.relname
                      FROM pg_catalog.pg_class c
                           LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
                      WHERE c.oid = table_rec.oid
                      ORDER BY 2, 3) b
                    ON a.attrelid = b.oid
                    INNER JOIN
                     (SELECT
                          a.attrelid,
                          max(a.attnum) as max_attnum
                      FROM pg_catalog.pg_attribute a
                      WHERE a.attnum > 0
                        AND NOT a.attisdropped
                      GROUP BY a.attrelid) e
                    ON a.attrelid=e.attrelid
                WHERE a.attnum > 0
                  AND NOT a.attisdropped
                ORDER BY a.attnum
            LOOP
                IF column_record.attnum = 1 THEN
                    v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' (';
                ELSE
                    v_table_ddl:=v_table_ddl||',';
                END IF;
    
                IF column_record.attnum <= column_record.max_attnum THEN
                    v_table_ddl:=v_table_ddl||chr(10)||
                             '    '||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null;
                END IF;
            END LOOP;
    
            firstrec := TRUE;
            FOR constraint_rec IN
                SELECT conname, pg_get_constraintdef(c.oid) as constrainddef
                    FROM pg_constraint c
                        WHERE conrelid=(
                            SELECT attrelid FROM pg_attribute
                            WHERE attrelid = (
                                SELECT oid FROM pg_class WHERE relname = table_rec.relname
                                    AND relnamespace = (SELECT ns.oid FROM pg_namespace ns WHERE ns.nspname = p_schema_name)
                            ) AND attname='tableoid'
                        )
            LOOP
                v_table_ddl:=v_table_ddl||','||chr(10);
                v_table_ddl:=v_table_ddl||'CONSTRAINT '||constraint_rec.conname;
                v_table_ddl:=v_table_ddl||chr(10)||'    '||constraint_rec.constrainddef;
                firstrec := FALSE;
            END LOOP;
            v_table_ddl:=v_table_ddl||');';
            RETURN NEXT v_table_ddl;
        END LOOP;
    END;
    $BODY$
      LANGUAGE plpgsql VOLATILE
      COST 100;
    
    0 讨论(0)
  • 2020-11-28 18:01

    Here is a query with some edits,

    select 'CREATE TABLE ' || a.attrelid::regclass::text || '(' ||
    string_agg(a.attname || ' ' || pg_catalog.format_type(a.atttypid, 
    a.atttypmod)||
            CASE WHEN 
                (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
                 FROM pg_catalog.pg_attrdef d
                 WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
                ' DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
                              FROM pg_catalog.pg_attrdef d
                              WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
            ELSE
                '' END
    ||
            CASE WHEN a.attnotnull = true THEN 
                ' NOT NULL'
            ELSE
                '' END,E'\n,') || ');' 
    FROM pg_catalog.pg_attribute a join pg_class on a.attrelid=pg_class.oid
    WHERE a.attrelid::regclass::varchar =  
    'TABLENAME_with_or_without_schema'
    AND a.attnum > 0 AND NOT a.attisdropped  and pg_class.relkind='r'
    group by a.attrelid;
    
    0 讨论(0)
  • 2020-11-28 18:02
    pg_dump -h XXXXXXXXXXX.us-west-1.rds.amazonaws.com -U anyuser -t tablename -s
    
    0 讨论(0)
  • 2020-11-28 18:03

    Generate the create table statement for a table in postgresql from linux commandline:

    This statement outputs the table create sql statement for me:

    pg_dump -U your_db_user_name your_database -t your_table_name --schema-only
    

    Explanation:

    pg_dump helps us get information about the database itself. -U stands for username. My pgadmin user has no password set, so I don't have to put in a password. The -t option means specify for one table. --schema-only means print only data about the table, and not the data in the table. Here is the exact command I use:

    pg_dump -U pgadmin kurz_prod -t fact_stock_info --schema-only
    
    0 讨论(0)
  • 2020-11-28 18:05
    pg_dump -t 'schema-name.table-name' --schema-only database-name
    

    More info - in the manual.

    0 讨论(0)
  • 2020-11-28 18:05

    Here is a bit improved version of shekwi's query.
    It generates the primary key constraint and is able to handle temporary tables:

    with pkey as
    (
        select cc.conrelid, format(E',
        constraint %I primary key(%s)', cc.conname,
            string_agg(a.attname, ', ' 
                order by array_position(cc.conkey, a.attnum))) pkey
        from pg_catalog.pg_constraint cc
            join pg_catalog.pg_class c on c.oid = cc.conrelid
            join pg_catalog.pg_attribute a on a.attrelid = cc.conrelid 
                and a.attnum = any(cc.conkey)
        where cc.contype = 'p'
        group by cc.conrelid, cc.conname
    )
    select format(E'create %stable %s%I\n(\n%s%s\n);\n',
        case c.relpersistence when 't' then 'temporary ' else '' end,
        case c.relpersistence when 't' then '' else n.nspname || '.' end,
        c.relname,
        string_agg(
            format(E'\t%I %s%s',
                a.attname,
                pg_catalog.format_type(a.atttypid, a.atttypmod),
                case when a.attnotnull then ' not null' else '' end
            ), E',\n'
            order by a.attnum
        ),
        (select pkey from pkey where pkey.conrelid = c.oid)) as sql
    from pg_catalog.pg_class c
        join pg_catalog.pg_namespace n on n.oid = c.relnamespace
        join pg_catalog.pg_attribute a on a.attrelid = c.oid and a.attnum > 0
        join pg_catalog.pg_type t on a.atttypid = t.oid
    where c.relname = :table_name
    group by c.oid, c.relname, c.relpersistence, n.nspname;
    

    Use table_name parameter to specify the name of the table.

    0 讨论(0)
提交回复
热议问题