备份: pg_dump -d m3vg -h localhost -p 5432 -U delta -W -f 1024.dump -F tar

恢复: pg_restore -h localhost -p 5432 -Udelta -W 1024.dump -d m3vg

忘记postgresql的用户的密码,怎么进入psql呢?

  1. sudo su postgres -

  2. psql

  执行上面两部就可以进入了

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

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}

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


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.

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
\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.

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: ERROR:  must be owner of schema public
psql:mydb.sql:34: ERROR: must be member of role "geekstuff"
psql:mydb.sql:59: WARNING: no privileges could be revoked
psql:mydb.sql:60: WARNING: no privileges could be revoked
psql:mydb.sql:61: WARNING: no privileges were granted
psql:mydb.sql:62: WARNING: no 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.

$ psql -f producttable.sql geekstuff

postgresql备份和恢复的更多相关文章

  1. PostgreSQL 备份和恢复

    备份和恢复有三种不同的基本方法来备份PostgreSQL数据SQL转储文件系统级备份File system level backup连续归档 1. SQL转储 pg_dump dbname > ...

  2. PostgreSQL备份和恢复数据表

    备份数据表: pg_dump -U user_name db_name -t table_name -f /tmp/backup_file.suffix 恢复数据表: 方法1: 登录数据库:\i /t ...

  3. postgresql数据库备份和恢复

    PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...

  4. PostgreSQL的备份和恢复

    关于PostgreSQL的备份和恢复详细信息请参阅<PostgreSQL中文文档>. 备份: #pg_dump --username=postgres v70_demo > v70_ ...

  5. postgresql数据库备份和恢复(超快)

    PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...

  6. PostgreSQL数据库常用脚本-初始化、备份、恢复推荐脚本

    公司最近开始逐步推广使用PostgreSQL,为方便开发人员和实施人员操作,特整理数据库初始化.备份.恢复的推荐脚本如下: 1. 连接数据库 psql -h 127.0.0.1 -p 1921 -U ...

  7. Gitlab备份和恢复操作记录

    前面已经介绍了Gitlab环境部署记录,这里简单说下Gitlab的备份和恢复操作记录: 1)Gitlab的备份目录路径设置 [root@code-server ~]# vim /etc/gitlab/ ...

  8. 转载:Gitlab备份和恢复操作记录

    转载:Gitlab备份和恢复操作记录 包含了备份和数据恢复的操作记录,实验可行 前面已经介绍了Gitlab环境部署记录,这里简单说下Gitlab的备份和恢复操作记录: 1)Gitlab的备份目录路径设 ...

  9. Gitlab+Jenkins学习之路(四)之gitlab备份和恢复

    gitlab的备份和恢复 (1)创建备份目录,并授权 [root@linux-node1 ~]# mkdir /data/backups/gitlab -p [root@linux-node1 ~]# ...

随机推荐

  1. ftp获取mysql数据库方法

    我说的这种情况是针对mysql数据库的,首先下载一个mysql通过ftp放到站点里面,然后通过配置文件找到数据库的名字和密码,然后通过浏览器访问数据库,直接在域名后面加上下载的mysql文件的名字就可 ...

  2. [ SDOI 2006 ] 仓库管理员的烦恼

    \(\\\) Description 有 \(n\) 种货物和 \(n\) 个仓库,开始第 \(i\) 个仓库里有 \(a_{ij}\) 个第 \(j\) 种货物. 现在要让每种货物都只放到一个仓库里 ...

  3. Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas)

    Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 如果还没有本地安装Python.IPython.notebook等请移步 上篇Py ...

  4. Selenium基于Python web自动化测试框架 -- PO

    关于selenium测试框架首先想到的就是PO模型,简单说下PO模型 PO模型的概念和理解: PO就是一个设计思想,将代码以页面为单位进行组织,针对这个页面上的所有信息.相关操作都放到一个类中,从而使 ...

  5. vue2.0路由(跳转和传参)经典介绍

    声明式 <router-link :to="...">编程式router.push(...) router.push('home')                 / ...

  6. 呼啦圈(keyframes和transform结合)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 06Microsoft SQL Server 完整性约束

    Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...

  8. 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet(转载)

    若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet 请将 J ...

  9. Python 操作excel day5

    一.Python操作excel python操作excel使用xlrd.xlwt和xlutils模块 1.xlrd模块是读取excel的: 2.xlwt模块是写excel的: 3.xlutils是用来 ...

  10. 字符串系列——KMP模板整理

    KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...