Sunteți pe pagina 1din 9

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

Backup Data to the Cloud See how Hitachi Cloud Storage can free up valuable I.T. resources. HDS.com/Cloud Oracle Tutor Training Learn Oracle Tutor - Instructor Led Fast Track Web based course vikat.ca

EMC Solutions for Oracle Want 44% More Performance and 90% More Efficiency? See How with EMC. www.emc.com/Oracle-Efficien

Home About Free eBook Archives Best of the Blog Contact

Ads by Google

Backup

Restore Deleted Data

Dump

Backup SQL Database

How To Backup and Restore PostgreSQL Database Using pg_dump and psql
by Ramesh Natarajan on January 21, 2009 This is a guest post written by SathiyaMoorthy pg_dump is an effective tool to backup postgres database. It creates a *.sql file with CREATE TABLE, ALTER TABLE, and COPY SQL statements of source database. To restore these dumps psql command is enough. Using pg_dump, you can backup a local database and restore it on a remote database at the same time, using a single command. In this article, let us review several practical examples on how to use pg_dump to backup and restore. For the impatient, here is the quick snippet of how backup and restore postgres database using pg_dump and psql:
Backup: $ pg_dump -U {user-name} {source_db} -f {dumpfilename.sql}

Restore: $ psql -U {user-name} -d {desintation_db}-f {dumpfilename.sql}

How To Backup Postgres Database


1. Backup a single postgres database
This example will backup erp database that belongs to user geekstuff, to the file mydb.sql
$ pg_dump -U geekstuff erp -f mydb.sql

1 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

It prompts for password, after authentication mydb.sql got created with create table, alter table and copy commands for all the tables in the erp database. Following is a partial output of mydb.sql showing the dump information of employee_details table.
--- Name: employee_details; Type: TABLE; Schema: public; Owner: geekstuff; Tablespace: -CREATE TABLE employee_details ( employee_name character varying(100), emp_id integer NOT NULL, designation character varying(50), comments text ); ALTER TABLE public.employee_details OWNER TO geekstuff; --- Data for Name: employee_details; Type: TABLE DATA; Schema: public; Owner: geekstuff -COPY employee_details (employee_name, emp_id, designation, comments) FROM stdin; geekstuff 1001 trainer ramesh 1002 author sathiya 1003 reader \. --- Name: employee_details_pkey; Type: CONSTRAINT; Schema: public; Owner: geekstuff; Tablespace: -ALTER TABLE ONLY employee_details ADD CONSTRAINT employee_details_pkey PRIMARY KEY (emp_id);

2. Backup all postgres databases


To backup all databases, list out all the available databases as shown below. Login as postgres / psql user:
$ su postgres

List the databases:


$ psql -l List of databases Name | Owner | Encoding -----------+-----------+---------article | sathiya | UTF8 backup | postgres | UTF8 erp | geekstuff | UTF8 geeker | sathiya | UTF8

Backup all postgres databases using pg_dumpall: You can backup all the databases using pg_dumpall command.
$ pg_dumpall > all.sql

Verify the backup: Verify whether all the databases are backed up,
$ grep "^[\]connect" all.sql \connect article \connect backup \connect erp

2 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

\connect geeker

3. Backup a specific postgres table


$ pg_dump --table products -U geekstuff article -f onlytable.sql

To backup a specific table, use the table TABLENAME option in the pg_dump command. If there are same table names in different schema then use the schema SCHEMANAME option.
Systems Biology Grants agilent.com/emerginginsights Agilent Bioinformatics Grants Genomics Transcriptomics Data Int. Reboot Restore Software www.RebootRestore.com Restore On Reboot + "Freeze Space" Area To Save Files From Restoration Build, Brand, and Bill www.longjump.com Whitelabeled SaaS App Platform to get you to market 10x faster PostgreSQL Solutions www.postgresql-support.de PostgreSQL Training, Support Replication, High-Availability

How To Restore Postgres Database


1. Restore a postgres database
$ psql -U erp -d erp_devel -f mydb.sql

This restores the dumped database to the erp_devel database. Restore error messages While restoring, there may be following errors and warning, which can be ignored.
psql:mydb.sql:13: psql:mydb.sql:34: psql:mydb.sql:59: psql:mydb.sql:60: psql:mydb.sql:61: psql:mydb.sql:62: ERROR: must ERROR: must WARNING: no WARNING: no WARNING: no WARNING: no be owner of schema public be member of role "geekstuff" privileges could be revoked privileges could be revoked privileges were granted privileges were granted

2. Backup a local postgres database and restore to remote server using single command:
$ pg_dump dbname | psql -h hostname dbname

The above dumps the local database, and extracts it at the given hostname.

3. Restore all the postgres databases


$ su postgres $ psql -f alldb.sql

4. Restore a single postgres table


The following psql command installs the product table in the geek stuff database.

3 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

$ psql -f producttable.sql geekstuff

This article was written by SathiyaMoorthy, developer of Enterprise Postgres Query Analyser, an efficient tool for parsing postgresql log to generate html report, which can be used for fine tuning the postgres settings, and sql queries. The Geek Stuff welcomes your tips and guest articles.

Bookmark/Share this Article Leave a Comment

If you enjoyed this article, you might also like..


1. 50 Linux Sysadmin Tutorials 2. 50 Most Frequently Used Linux Commands (With Examples) 3. Mommy, I found it! 15 Practical Linux Find Command Examples 4. Turbocharge PuTTY with 12 Powerful Add-Ons 5. 12 Amazing and Essential Linux Books To Enrich Your Brain 5 Best Wireless Routers 5 Best Digital SLR Cameras 5 Best Portable GPS Navigators for Cars 5 Best High Definition Digital Camcorders 5 Best Point and Shoot Digital Cameras

Tags: backup, Backup PostgreSQL Database, pg_dump, pg_dump command, pg_dumpall, pg_dumpall command, Postgres, Postgres database backup, Postgres DB Backup, Postgres DB Restore, PostgreSQL database, psql, psql command, restore, Restore PostgreSQL Database

4 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

{ 8 comments read them below or add one } 1 user January 22, 2009 at 5:56 am simple and best 2 Crovar January 22, 2009 at 11:22 am Nice article 3 rocio August 3, 2009 at 10:55 am please, tell how can i backup and restore only the user roles, i tryed to use pg_dumpall but I couldn`t restore the file 4 augustine October 2, 2009 at 9:24 am shall try the commands given then i shall modrate 5 Selvaganeshan October 11, 2009 at 11:52 pm Can i take backup for two tables in single command line pg_dump -t table1 -t table2 -U db db -f table.sql Above command is taking dump for only table2 6 shikin August 12, 2010 at 11:19 pm hii just backup all postgres on linux and what to restore to postgres window -how to restore to postgres window if i use pg_dumpall command on linux? -how can i get the data backup? -how to i restore the backup on postgres window? Please help me. 7 Saad September 21, 2010 at 1:38 am This is very easy understandable article. i want to take backup of some tables from another pg server and then want to restore it another server , can any one help me ? Saad 8 Lacy October 10, 2010 at 5:35 am Thanks for this great article. Easy to understand. Leave a Comment Name E-mail Website

5 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

Notify me of followup comments via e-mail

Previous post: Free eBook: Linux 101 Hacks Next post: Overview Of PoE Power Over Ethernet Concepts and Devices List Sign up for our free email newsletter you@address.com RSS Twitter Facebook
Sign Up

Search
Systems Biology Grants Agilent Bioinformatics Grants Genomics Transcriptomics Data Int.
agilent.com/emerginginsights

PostgreSQL Solutions PostgreSQL Training, Support Replication, High-Availability


www.postgresql-support.de

EMC Solutions for Oracle Gain 90% More Efficiency with EMC Solutions for Oracle. Free Overview
www.emc.com/Oracle-Efficiency

Reboot Restore Software Restore On Reboot + "Freeze Space" Area To Save Files From Restoration
www.RebootRestore.com

EBOOKS

6 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

Systems Biology Grants Agilent Bioinformatics Grants Genomics Transcriptomics Data Int.
agilent.com/emerginginsights

EMC Solutions for Oracle Want 44% More Performance and 90% More Efficiency? See How with EMC.
www.emc.com/Oracle-Efficiency

Build, Brand, and Bill Whitelabeled SaaS App Platform to get you to market 10x faster
www.longjump.com

PHP Code Generator Rapid WEB application development Forms, Reports, Grids, Charts, PDF.
www.scriptcase.net

POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library 50 UNIX / Linux Sysadmin Tutorials 50 Most Frequently Used UNIX / Linux Commands (With Examples) How To Be Productive and Get Things Done Using GTD 30 Things To Do When you are Bored and have a Computer Linux Directory Structure (File System Structure) Explained with Examples Linux Crontab: 15 Awesome Cron Job Examples Get a Grip on the Grep! 15 Practical Grep Command Examples Unix LS Command: 15 Practical Examples 15 Examples To Master Linux Command Line History Top 10 Open Source Bug Tracking System Vi and Vim Macro Tutorial: How To Record and Play Mommy, I found it! -- 15 Practical Linux Find Command Examples 15 Awesome Gmail Tips and Tricks 15 Awesome Google Search Tips and Tricks RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams Can You Top This? 15 Practical Linux Top Command Examples Top 5 Best System Monitoring Tools Top 5 Best Linux OS Distributions How To Monitor Remote Linux Host using Nagios 3.0 Awk Introduction Tutorial 7 Awk Print Examples How to Backup Linux? 15 rsync Command Examples The Ultimate Wget Download Guide With 15 Awesome Examples Top 5 Best Linux Text Editors Packet Analyzer: 15 TCPDUMP Command Examples The Ultimate Bash Array Tutorial with 15 Examples 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id Unix Sed Tutorial: Advanced Sed Substitution Examples UNIX / Linux: 10 Netstat Command Examples The Ultimate Guide for Creating Strong Passwords 6 Steps to Secure Your Home Wireless Network Turbocharge PuTTY with 12 Powerful Add-Ons

7 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Support Us
Support this blog by purchasing one of my ebooks. Nagios Core 3 eBook: Monitor Everything, Be Proactive, and Sleep Well. Vim 101 Hacks eBook: Practical Examples for Becoming Fast and Productive in the Vim Editor.

Contact Us

8 de 9

21/06/2011 07:14 p.m.

How To Backup and Restore PostgreSQL Database Using pg_dump and...

http://www.thegeekstuff.com/2009/01/how-to-backup-and-restore-postg...

Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!. Follow us on Twitter Become a fan on Facebook Copyright 20082011 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise
PostgreSQL Solutions PostgreSQL Training, Support Replication, High-Availability www.postgresql-support.de Reboot Restore Software Restore On Reboot + "Freeze Space" Area To Save Files From Restoration www.RebootRestore.com SQL Server Forum Share Knowledge and Ask Questions on SQL Servers at Toolbox for IT. Database.ittoolbox.com

9 de 9

21/06/2011 07:14 p.m.

S-ar putea să vă placă și