postgresql_16:7.2.16.6 Examples
PostgreSQL 16 > Reference > PostgreSQL Client Applications > pg_restore > Examples
Assume we have dumped a database called mydb into a custom-format dump file: $ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump: $ dropdb mydb $ pg_restore -C -d postgres db.dump The database named in the -d switch can be any database existing in the cluster; pg_restore only uses it to issue the CREATE DATABASE command for mydb. With -C, data is always restored into the database name that appears in the dump file.
To restore the dump into a new database called newdb: $ createdb -T template0 newdb $ pg_restore -d newdb db.dump Notice we don't use -C, and instead connect directly to the database to be restored into. Also note that we clone the new database from template0 not template1, to ensure it is initially empty.
To reorder database items, it is first necessary to dump the table of contents of the archive: $ pg_restore -l db.dump > db.list The listing file consists of a header and one line for each item, e.g.: ; ; Archive created at Mon Sep 14 13:55:39 2009 ; dbname: DBDEMOS ; TOC Entries: 81 ; Compression: 9 ; Dump Version: 1.10-0 ; Format: CUSTOM ; Integer: 4 bytes ; Offset: 8 bytes ; Dumped from database version: 8.3.5 ; Dumped by pg_dump version: 8.3.8 ; ; ; Selected TOC Entries: ; 3; 2615 2200 SCHEMA - public pasha 1861; 0 0 COMMENT - SCHEMA public pasha 1862; 0 0 ACL - public pasha 317; 1247 17715 TYPE public composite pasha 319; 1247 25899 DOMAIN public domain0 pasha Semicolons start a comment, and the numbers at the start of lines refer to the internal archive ID assigned to each item.
Lines in the file can be commented out, deleted, and reordered. For example: 10; 145433 TABLE map_resolutions postgres ;2; 145344 TABLE species postgres ;4; 145359 TABLE nt_header postgres 6; 145402 TABLE species_records postgres ;8; 145416 TABLE ss_old postgres could be used as input to pg_restore and would only restore items 10 and 6, in that order: $ pg_restore -L db.list db.dump