PostgreSQL database backup and restoration command

Source: Internet
Author: User
Tags postgresql psql create database

Pg_dump backup data

-- You only need to enter the name of the database to be exported after the command prompt to back up the database, as shown in the following example (change the PostgreSQL path based on your own installation path ):

The code is as follows: Copy code

Xiaop @ xiaop-laptop :~ $/Usr/lib/postgresql/8.2/bin/pg_dump-D-h localhost-U xiaop (user name) mydb (database name)> mydb. bak

The preceding command creates a file named mydb. bak, which records the SQL commands used to restore the database.


Restore with pg_dumpall;

If you use pg_dumpall to back up all the databases, there is no need to CREATE a new DATABASE because the backup file contains the call related to completing the create database operation. Here, you only need to enter the corresponding backup file in the psql command line client, instead of specifying the target database:

The code is as follows: Copy code

Xiaop @ xiaop-laptop :~ $/Usr/lib/postgresql/8.2/bin/psql-h localhost-U xiaop (user name) <all. bak

After data recovery is complete

How to export data from a PostgreSQL database:

The code is as follows: Copy code

$ Pg_dump-U postgres-d mydatabase-f dump. SQL

When importing data, first create a database and then use psql to import the data:

The code is as follows: Copy code

$ Createdb newdatabase
$ Psql-d newdatabase-U postgres-f dump. SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.