postgresql备份和恢复
备份: 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备份和恢复的更多相关文章
- PostgreSQL 备份和恢复
备份和恢复有三种不同的基本方法来备份PostgreSQL数据SQL转储文件系统级备份File system level backup连续归档 1. SQL转储 pg_dump dbname > ...
- PostgreSQL备份和恢复数据表
备份数据表: pg_dump -U user_name db_name -t table_name -f /tmp/backup_file.suffix 恢复数据表: 方法1: 登录数据库:\i /t ...
- postgresql数据库备份和恢复
PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...
- PostgreSQL的备份和恢复
关于PostgreSQL的备份和恢复详细信息请参阅<PostgreSQL中文文档>. 备份: #pg_dump --username=postgres v70_demo > v70_ ...
- postgresql数据库备份和恢复(超快)
PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...
- PostgreSQL数据库常用脚本-初始化、备份、恢复推荐脚本
公司最近开始逐步推广使用PostgreSQL,为方便开发人员和实施人员操作,特整理数据库初始化.备份.恢复的推荐脚本如下: 1. 连接数据库 psql -h 127.0.0.1 -p 1921 -U ...
- Gitlab备份和恢复操作记录
前面已经介绍了Gitlab环境部署记录,这里简单说下Gitlab的备份和恢复操作记录: 1)Gitlab的备份目录路径设置 [root@code-server ~]# vim /etc/gitlab/ ...
- 转载:Gitlab备份和恢复操作记录
转载:Gitlab备份和恢复操作记录 包含了备份和数据恢复的操作记录,实验可行 前面已经介绍了Gitlab环境部署记录,这里简单说下Gitlab的备份和恢复操作记录: 1)Gitlab的备份目录路径设 ...
- Gitlab+Jenkins学习之路(四)之gitlab备份和恢复
gitlab的备份和恢复 (1)创建备份目录,并授权 [root@linux-node1 ~]# mkdir /data/backups/gitlab -p [root@linux-node1 ~]# ...
随机推荐
- 数据采集框架Gobblin简介
问题导读: Gobblin的架构设计是怎样的? Gobblin拥有哪些组建,如何实现可扩展? Gobblin采集执行流程的过程? 前面我们介绍Gobblin是用来整合各种数据源的通用型ETL框架,在某 ...
- data和string类型之间的相互转换
package main; import java.text.SimpleDateFormat;import java.util.Date; import freemarker.core.ParseE ...
- js解析地址栏参数
/** * 获取地址栏中url后面拼接的参数 * eg: * 浏览器地址栏中的地址:http://1.1.1.1/test.html?owner=2db08226-e2fa-426c-91a1-66e ...
- Android ListView动态刷新某项Item
使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...
- grunt 全局使用
grunt 不同地方使用时需要将插件下载的当前文件夹,这是因为查找模块时是当前路径,这会造成多个工程使用时会需要下载多次,而这些东西又不应该存在于工程之中,所以应该将所有模块全局安装,然后在工程下面只 ...
- Java 基础入门随笔(8) JavaSE版——静态static
面向对象(2) this:代表对象.代表哪个对象呢?当前对象. 当成员变量和局部变量重名,可以用关键字this来区分. this就是所在函数所属对象的引用.(简单说:哪个对象调用了this所在的函数, ...
- Python 开发初识
从今天开始记录自己的python开发之路,用博客记录自己的学习经历,以及学习小结,小的项目模块,努力充实,做最好的自己!!!
- 实现加载页Loading Page 的几种方法
网页也可以像原生应用那样加入进度条或者其他的loading效果带来更好的等待体验,这里归纳几种我收集的实现loading page的方法,这几种方法在交互上都有利有弊,适用于不同应用.(PS:以下方法 ...
- 微服务网关从零搭建——(二)搭建api网关(不带验证)
环境准备 创建空的core2.1 api项目 演示使用名称APIGateWay 过程参考上一篇 完成后在appsettings.json 添加节点 "Setting": { & ...
- Extjs二级联动combo省城市
Extjs二级联动 Extjs combox根据省查询城市 实现效果如上图所示, store层代码: Ext.define("ExtApp.store.TeacherProvince&quo ...