How can I export the schema of a database in PostgreSQL?
NickName:programmer Ask DateTime:2013-01-24T01:57:18

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

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.

Copyright Notice:Content Author:「programmer」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14486241/how-can-i-export-the-schema-of-a-database-in-postgresql

Answers
arod 2015-05-28T18:21:38

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


Hayk Petrosyan 2018-06-25T16:59:34

You should use something like this pg_dump --schema=your_schema_name db1, for details take a look here",


k0w 2020-11-05T12:54:03

pg_dump -s databasename -t tablename -U user -h host -p port > tablename.sql\nthis will limit the schema dump to the table "tablename" of "databasename"",


Anew 2013-01-23T18:13:08

You should take a look at pg_dump:\npg_dump --schema-only databasename\n\nWill dump only the schema to stdout as .sql.\nFor 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.",


James Jithin 2017-08-02T05:05:33

I am running Postgres 9.6 where I had to export a particular schema along with data.\n\nI used the following command:\n\npg_dump.exe -U username -d databasename -n schemaname > C:\\mylocation\\mydumpfilename.dmp\n\n\nIf you want only the schema without data, use the switch s instead of n\n\nBelow is the pg_dump switch list:\n\nC:\\Program Files\\PostgreSQL\\9.6\\bin>pg_dump --help\npg_dump dumps a database as a text file or to other formats.\n\nUsage:\n pg_dump [OPTION]... [DBNAME]\n\nGeneral options:\n -f, --file=FILENAME output file or directory name\n -F, --format=c|d|t|p output file format (custom, directory, tar,\n plain text (default))\n -j, --jobs=NUM use this many parallel jobs to dump\n -v, --verbose verbose mode\n -V, --version output version information, then exit\n -Z, --compress=0-9 compression level for compressed formats\n --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n -?, --help show this help, then exit\n\nOptions controlling the output content:\n -a, --data-only dump only the data, not the schema\n -b, --blobs include large objects in dump\n -c, --clean clean (drop) database objects before recreating\n -C, --create include commands to create database in dump\n -E, --encoding=ENCODING dump the data in encoding ENCODING\n -n, --schema=SCHEMA dump the named schema(s) only\n -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)\n -o, --oids include OIDs in dump\n -O, --no-owner skip restoration of object ownership in\n plain-text format\n -s, --schema-only dump only the schema, no data\n -S, --superuser=NAME superuser user name to use in plain-text format\n -t, --table=TABLE dump the named table(s) only\n -T, --exclude-table=TABLE do NOT dump the named table(s)\n -x, --no-privileges do not dump privileges (grant/revoke)\n --binary-upgrade for use by upgrade utilities only\n --column-inserts dump data as INSERT commands with column names\n --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n --disable-triggers disable triggers during data-only restore\n --enable-row-security enable row security (dump only content user has\n access to)\n --exclude-table-data=TABLE do NOT dump data for the named table(s)\n --if-exists use IF EXISTS when dropping objects\n --inserts dump data as INSERT commands, rather than COPY\n --no-security-labels do not dump security label assignments\n --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n --no-tablespaces do not dump tablespace assignments\n --no-unlogged-table-data do not dump unlogged table data\n --quote-all-identifiers quote all identifiers, even if not key words\n --section=SECTION dump named section (pre-data, data, or post-data)\n --serializable-deferrable wait until the dump can run without anomalies\n --snapshot=SNAPSHOT use given snapshot for the dump\n --strict-names require table and/or schema include patterns to\n match at least one entity each\n --use-set-session-authorization\n use SET SESSION AUTHORIZATION commands instead of\n ALTER OWNER commands to set ownership\n\nConnection options:\n -d, --dbname=DBNAME database to dump\n -h, --host=HOSTNAME database server host or socket directory\n -p, --port=PORT database server port number\n -U, --username=NAME connect as specified database user\n -w, --no-password never prompt for password\n -W, --password force password prompt (should happen automatically)\n --role=ROLENAME do SET ROLE before dump\n\nIf no database name is supplied, then the PGDATABASE environment\nvariable value is used.\n\nReport bugs to <[email protected]>.\n",


lev09 2015-07-24T05:41:06

In Linux you can do like this\n\npg_dump -U postgres -s postgres > exportFile.dmp\n\n\nMaybe it can work in Windows too,\nif not try the same with pg_dump.exe \n\npg_dump.exe -U postgres -s postgres > exportFile.dmp\n",


alfons 2018-11-21T10:18:54

pg_dump -d <databasename> -h <hostname> -p <port> -n <schemaname> -f <location of the dump file>\n\n\nPlease notice that you have sufficient privilege to access that schema.\nIf you want take backup as specific user add user name in that command preceded by -U ",


Lalit Bangad 2020-05-04T19:20:35

For Linux: (data excluded)\n\n\npg_dump -s -t tablename databasename > dump.sql (For a specific table in database)\npg_dump -s databasename > dump.sql (For the entire database)\n",


More about “How can I export the schema of a database in PostgreSQL?” related questions

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

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 st...

Show Detail

How do I export a postgresql database schema to an XML format?

Uh... How do I export a postgresql database schema to an XML format?

Show Detail

How to copy Postgresql database schema to prisma schema

I am using prisma and PostgreSQL. My PostgreSQL database filled with some data. Is it possible to get Postgresql database schema and set it to prisma schema or something? I've tried to prisma db pu...

Show Detail

Export data types from PostgreSQL schema

I'd like to export the data types from a PostgreSQL specific schema. The problem is, for now I found just a way to export the whole schema and not just the data types. The schema has more than 2000

Show Detail

Install Drupal database in specific PostGreSQL schema

I want to install &amp; setup my Drupal databases in a specific schema of the PostgreSQL database. For example, let's say I have created a schema named "test_drupal" in my existing PostGreSQL datab...

Show Detail

PostgreSQL: Create schema in specific database [NOT psql]

Im trying to create a SQL file to import my database/schema/tables with PHP. If I use the PgAdmin, it inserts a meta-command \connect after the CREATE TABLE and this generates an error if I run th...

Show Detail

Converting SQL Server 2012 schema to PostgreSQL 9.1

For a new project we have to export data from a SQL Server 2012 database to a PostgreSQL database. We have the SQL Server schema but have to create one for PostgreSQL. As far as possible we would...

Show Detail

PostgreSQL: Create schema in specific database

I need to write an sql script that creates both a new database AND a new schema in the database I just created. How can I do it? Can I somehow change the current database to the new one? Or can I

Show Detail

Dump PostgreSQL database schema with Python

I'm looking for a way to dump a PostgreSQL database schema using Python. Ideally, the result would be either a .sql dump or any other format that can later be used by SQLAlchemy to create a new dat...

Show Detail

How to export database from PostgreSQL?

I am working on a team project in object-relational databases and one of us has made the database in PostgreSQL, and the rest of us will make the application based on that database. How can he expo...

Show Detail