Dumping and Restoring PostgreSQL Database

Dumping is a process of making a file containing SQL queries that can be used to contruct the whole database. Dumping is commonly used to download your database for backup or other purposes. Restoring is the reverse process of dumping.

Dumping PostgreSQL Database

To dump your PostgreSQL database you need to be logged on to your shell account. Please see the section called “Logging in to UNIX Shell Account” for more information about logging on shell account. Assuming your database name is u777_database, you need to use the following command to dump your PostgreSQL database.

pg_dump -U u777_database u777_database \
    > u777_database.sql

That command should create a file named u777_database.sql in the current directory. You can use FTP or other means to download the file.

To make a gzip compressed dump file, you can use a few modification to the above command.

pg_dump -U u777_database u777_database \
    | gzip > u777_database.sql.gz

Restoring PostgreSQL Database

To restore previously dumped database you need to use psql.

psql -U u777_database u777_database -f - \
    < u777_database.sql

Similarly, you can use the following command to restore PostgreSQL database when the dumped file is gzip compressed.

gunzip < u777_database.sql | \
    psql -U u777_database u777_database -f -

Tip

In order to use bzip2 instead of gzip compression, you need to substitute gzip with bzip2 and gunzip with bunzip2 respectively.

Copyright © 2003 indoglobal.com

. .