热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Postgresql备份和恢复的方法及命令行操作步骤

本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-hlocalhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

更多详情参考 https://stackoverflow.com/questions/2732474/restore-a-postgres-backup-file-using-the-command-line

1. 110 down vote

create backup

pg_dump -i -h localhost -p 5432 -U postgres -F c -b -v -f
"/usr/local/backup/10.70.0.61.backup" old_db

restore from backup

pg_restore -i -h localhost -p 5432 -U postgres -d old_db -v
"/usr/local/backup/10.70.0.61.backup"

important to set -h localhost - option

---------------------------------------------------------

2. 50 down vote

You might need to be logged in as postgres in order to have full privileges on databases.

su - postgres
psql -l # will list all databases on Postgres cluster

pg_dump/pg_restore

pg_dump -U username -f backup.dump database_name -Fc

switch -F specify format of backup file:

  • c will use custom PostgreSQL format which is compressed and results in smallest backup file size
  • d for directory where each file is one table
  • t for TAR archive (bigger than custom format)

restore backup:

pg_restore -d database_name -U username -C backup.dump

Parameter -C should create database before importing data. If it doesn't work you can always create database eg. with command (as user postgres or other account that has rights to create databases) createdb db_name -O owner

pg_dump/psql

In case that you didn't specify the argument -F default plain text SQL format was used (or with -F p). Then you can't use pg_restore. You can import data with psql.

backup:

pg_dump -U username -f backup.sql database_name

restore:

psql -d database_name -f backup.sql

----------------------------------------------------------------------------------

3. 31 down vote

POSTGRESQL 9.1.12

DUMP:

pg_dump -U user db_name > archive_name.sql

put the user password and press enter.

RESTORE:

psql -U user db_name

put the user password and press enter.

-----------------------------------------------------------------------------------------

Below is my version of pg_dump which I use to restore the database:

pg_restore -h localhost -p 5432 -U postgres -d my_new_database my_old_database.backup

or use psql:

psql -h localhost -U postgres -p 5432 my_new_database

where -h host, -p port, -u login username, -d name of database

-------------------------------------------------------------------------------------------

4. 7 down vote

Backup and restore with GZIP

For larger size database this is very good

backup

pg_dump -U user -d mydb | gzip > mydb.pgsql.gz

resore

gunzip -c mydb.pgsql.gz | psql dbname -U user

https://www.postgresql.org/docs/9.1/static/backup-dump.html

---------------------------------------------------------------------------------------------

 

 


转:https://my.oschina.net/jms0755/blog/1928420



推荐阅读
author-avatar
只为_sunny
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有