Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a schema only backup and restore in PostgreSQL?

People also ask

How do I restore a schema in PostgreSQL?

In PostgreSQL, you can restore a database in two ways: Using psql to restore plain SQL script file generated by pg_dump and pg_dumpall tools. Using pg_restore to restore tar file and directory format created by the pg_dump tool.

How do I backup a schema?

Run the db_backup command to back up an entire database, or just a schema. In addition to restoring data by using the db_restore command, you can use the web console for data restore. Run the db_restore command to restore a database, a schema, or a table that was backed up using the db_backup command.

How do I backup and restore a PostgreSQL database?

To back up, a PostgreSQL database, start by logging into your database server, then switch to the Postgres user account, and run pg_dump as follows (replace tecmintdb with the name of the database you want to backup). By default, the output format is a plain-text SQL script file.


pg_dump --schema=masters oldDB > masters1.sql
cat masters1.sql | psql newDB

or

in single command you can do by this

pg_dump oldDB --schema masters  | psql -h localhost newDB;

Backup schema and restore it on system for postgresql as below:

Dump schema for database

pg_dump -s database_name > db.sql

Dump schema for specific table

pg_dump -s database_name -t table_name > db.sql 

Restore backed up schema using below command

psql -d database_name -h localhost -U postgres < path/db.sql

What's wrong with the documentation?

Example from the manual:

To dump all schemas whose names start with east or west and end in gsm, excluding any schemas whose names contain the word test:

$ pg_dump -n 'east*gsm' -n 'west*gsm' -N 'test' mydb > db.sql


-s or --schema-only to exclude data from dump Documentation