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 |