Postgresql – Getting Postgres CREATE TABLE statements

djangopg-dumppostgresql

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, 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.?

Best Answer

I don't know of any Postgres-specific tool that can do that, maybe you should approach this from the Django perspective: https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-sql