
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) 'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) Pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type, The following plpgsql function: CREATE OR REPLACE FUNCTION generate_create_table_statement(p_table_name varchar) For create table of one public.tablenameīased on the sql echoed out after running these describe commands, I was able to put together The describe table statement: - List all tables in the schema (my example schema name is public)

In psql, run the following commands to see the sql that postgres uses to generate My solution is to log in to the postgres db using psql with the -E option as follows: psql -E -U username -d database

(NOTICE - this solution is not working with PostgreSQL v12+)
