HomeLinuxHow To Restore a Schema from a PostgreSQL Database

How To Restore a Schema from a PostgreSQL Database


If you happen to intend to revive just one or a number of schemas from a PostgreSQL backup file, you should utilize the pg_restore command, which is used for restoring a selected PostgreSQL database from an archive created by pg_dump in non-plain-text codecs.

On this information, we’ll present the way to restore a selected schema from a PostgreSQL database backup file utilizing the pg_restore command-line device.

Restoring Database Schema from PostgreSQL Database

Right here is an instance pg_restore command that restores a specific schema from a PostgreSQL database backup file:

Let’s have a look at the that means of every possibility within the above command:

  • -d – defines the goal database title which should exist on the server, pg_restore connects to it and restores immediately into the database.
  • -n or --schema – defines the title of the schema to be restored, it instructs pg_restore to revive solely objects which can be within the named schema.
  • backup.dump – the title of the database backup file. On this case, the backup is in a customized format, one of many codecs supported by the pg_dump device.

Restoring A number of Schemas from PostgreSQL Database

To revive a number of schemas, use a number of -n as proven.

$ pg_restore -d testdb -n schema_name1 -n schema_name2 -n schema_name3 backup.dump
OR
$ pg_restore -d testdb --schema=schema_name1 --schema=schema_name2 --schema=schema_name3 backup.dump

If you’re restoring the backup file on a brand new server, be sure that the proprietor or person of the database as outlined within the backup is created on the server earlier than the restoration course of is initiated.

pg_restore Command Choices

There are a number of different precious pg_restore command-line choices that you should utilize whereas performing a database restoration, we’ll cowl a number of beneath.

One helpful possibility is the -C or --create possibility which you should utilize to instruct pg_restore to create the database (specified utilizing the -d possibility) in case it doesn’t exist on the cluster earlier than restoring it.

Right here is an instance command:

$ pg_restore -d testdb -C -n schema_name  backup.dump
OR
$ pg_restore -d testdb --create -n schema_name backup.dump

Notice: When the -C possibility is employed, the database title testdb (within the above command) is simply used to run the preliminary “DROP DATABASE testdb” and “CREATE DATABASE testdb” instructions, however the knowledge is restored into the database title that seems within the backup file.

Moreover, in the event you use the --clean possibility, pg_restore will clear (drop) and recreate the goal database earlier than connecting to it.

$ pg_restore --clean -d testdb -n schema_name backup.dump

Moreover, you may also specify the variety of jobs to run concurrently whereas performing the restoration, utilizing the -j or --number-of-jobs. This flag tells pg_restore to run time-consuming steps akin to loading knowledge, creating indexes, or creating constraints concurrently utilizing concurrent classes of as much as the required variety of jobs:

$ pg_restore -j 4 --clean -d testdb -n schema_name backup.dump
OR
$ pg_restore --number-of-jobs=4 --clean -d testdb -n schema_name backup.dump

The above possibility is affected by {hardware} elements such because the variety of CPU cores and disk setup on the server, consumer, and community. Moreover, it solely helps the customized and listing archive codecs.

For extra data, take a look at the pg_restore man web page as proven.

$ man pg_restore

That’s all! pg_restore is without doubt one of the useful command-line instruments for the PostgreSQL database administration system. On this article, we’ve checked out the way to restore a selected schema from a PostgreSQL database backup file.

For any queries or feedback regarding this information, use the suggestions kind beneath to succeed in us.

If You Admire What We Do Right here On TecMint, You Ought to Take into account:

TecMint is the quickest rising and most trusted group website for any form of Linux Articles, Guides and Books on the internet. Hundreds of thousands of individuals go to TecMint! to go looking or browse the hundreds of revealed articles out there FREELY to all.

If you happen to like what you’re studying, please take into account shopping for us a espresso ( or 2 ) as a token of appreciation.

Support Us

We’re grateful on your by no means ending assist.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments