How can I export the schema of a database in PostgreSQL?

programmer picture programmer · Jan 23, 2013 · Viewed 184.4k times · Source

My computer broke down but fortunately I backed up the folder C:\Program Files\PostgreSQL.

Now I'm working in a new computer and I would like to import the previous Postgres databases that are stored in the external disk.

I would like to export the schema of a specific database that is located in the backup folder.

The file PostgreSQL\8.3\data\global\pg_database contains information about databases and their OIDs; for example:

"db1" 20012
"db2" 23456

I would like to export the schema of "db1".

There is a folder named "20012" in folder "PostgreSQL\8.3\data\base\20012" that contains a lot of files [500 files].

Is there any way to export the schema of that database?

Note that all of the Postgresql database files are located in an external hard disk and I would like to export the schema of that database in an SQL file, take that file, run it and create the same exact database locally.

Answer

Anew picture Anew · Jan 23, 2013

You should take a look at pg_dump:

pg_dump -s databasename

Will dump only the schema to stdout as .sql.

For windows, you'll probably want to call pg_dump.exe. I don't have access to a Windows machine but I'm pretty sure from memory that's the command. See if the help works for you too.