Sample Header Ad - 728x90

Getting Postgres CREATE TABLE statements

3 votes
2 answers
663 views
I created some tables (9, to be exact) with a Django (1.9.6) migration and now I'm trying to get simple CREATE TABLE statements for them. I tried [this answer](https://stackoverflow.com/a/2594564/3704831) , but using pg_dump in this way gives me over 800 lines of output for the 9 tables. For example, part of the output creating the first table is -- -- Name: popresearch_question; Type: TABLE; Schema: public; Owner: postgres -- CREATE TABLE popresearch_question ( id integer NOT NULL, created_date timestamp with time zone NOT NULL, modified_date timestamp with time zone NOT NULL, question_text character varying(500) NOT NULL, question_template_id integer, question_type_id integer, user_id integer ); ALTER TABLE popresearch_question OWNER TO postgres; -- -- Name: popresearch_question_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres -- CREATE SEQUENCE popresearch_question_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1; ALTER TABLE popresearch_question_id_seq OWNER TO postgres; -- -- Name: popresearch_question_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres -- ALTER SEQUENCE popresearch_question_id_seq OWNED BY popresearch_question.id; and then later on are more ALTER statements: -- -- Name: popresearch_question id; Type: DEFAULT; Schema: public; Owner: postgres -- ALTER TABLE ONLY popresearch_question ALTER COLUMN id SET DEFAULT nextval('popresearch_question_id_seq'::regclass); and then later: -- -- Name: popresearch_question popresearch_question_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres -- ALTER TABLE ONLY popresearch_question ADD CONSTRAINT popresearch_question_pkey PRIMARY KEY (id); -- -- Name: popresearch_question popresearch_question_question_text_key; Type: CONSTRAINT; Schema: public; Owner: postgres -- ALTER TABLE ONLY popresearch_question ADD CONSTRAINT popresearch_question_question_text_key UNIQUE (question_text); and after that there are at least a dozen more ALTER TABLE statements just for this one table scattered in the pg_dump output. Is there a way to get a simple, condensed CREATE TABLE statement that includes all the keys, constraints, etc.?
Asked by wogsland (416 rep)
May 9, 2017, 09:47 PM
Last activity: Jul 3, 2025, 07:00 PM