How can I do a dump of only the table structure in PostgreSQL?

60,268

Solution 1

pg_dump --schema-only

Solution 2

If you only want the CREATE TABLE statements, then you can do pg_dump -s databasename | awk 'RS="";/CREATE TABLE[^;]*;/'

Share:
60,268

Related videos on Youtube

warren
Author by

warren

I'm a hobbyist programmer, part-time sysadmin, and full-time analytics, big data, data center management, automation, and cloud computing architect and delivery engineer.

Updated on September 17, 2022

Comments

  • warren
    warren over 1 year

    In a similar vein to this question, how would I do a schema-only dump in PostgreSQL?

  • warren
    warren over 14 years
    easier than I realized it would be :)
  • jldugger
    jldugger over 14 years
    Frankly, I haven't much clue about postgres administration, but I've been watching videos from pgcon 09 and someone mentioned pg_dump.
  • warren
    warren over 14 years
    either way - it's what I needed :)
  • hans0l0
    hans0l0 over 10 years
    @warren: it's even easier: pg_dump -s.
  • Dave
    Dave about 7 years
    Not sure why the downvotes as this command works perfectly. So +1 from me.
  • Arne
    Arne over 5 years
    @Dave because adding an additional option not at all linked to the initial question 6 years later is not that useful.
  • elrobis
    elrobis almost 5 years
    @Arne. Disagree. The question title, which specifies dumping "only the table structure", gives this page a high search rank. And plenty of people, like Natan who remarked prior to you, want to avoid moving functions and/or views from old extensions in the event that they are starting with a fresh db with updated extensions. So it absolutely is "useful", as opposed to "not useful", which are the voting criteria. Absolutely getting my +1.
  • Emilio Platzer
    Emilio Platzer over 4 years
    @Dave. I think that this answer is right. I opened because the title. I'm searching table-only struct dump. This is better than the other or the title is not acurated.