How to dump and restore a Postgres database

Written by - 0 comments

Published on - Listed in Linux Database Postgres


First make sure, that the local postgres user has full rights on the Postgres server (trust):

cat pg_hba.conf
# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# "local" is for Unix domain socket connections only
local   all         all                               trust
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust

DUMP
Now we create the full dump (structure and data) of the database DB1:

pg_dump -p 5432 -h localhost -U postgres -Fc -f /tmp/pgdump.sql DB1

And this is for a single table 'table1' in DB1:

pg_dump -Fc -t table1 DB1 > /tmp/table1.dmp

RESTORE
To restore the dump several steps need to be done:

1. Create the database DB1 manually if database doesn't exist yet (e.g. if you want to restore the sql-dump on another server)

2. Don't forget to create a role if a special user (not postgres) is the owner of the database.

3. Launch the restore command:

pg_restore -p 5432 -h localhost -U postgres -d DB1 -Fc /tmp/pgdump.sql


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   OpenSearch   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder