当我尝试在我的devel数据库节点上恢复一个大型数据库(几乎是32Go自定义格式)时,我遇到了一个小问题(这个节点的RAM,CPU比我的生产服务器少).
我的数据库转储使用类似于以下的命令生成:
pg_dump -F custom -b myDB -Z 9 > /backup/myDB-`date +%y%m%d`.pg91
Run Code Online (Sandbox Code Playgroud)
当我恢复它时,我使用以下命令:
pg_restore -F custom -j 5 -d myDB /backup/myDB-20130331.pg91
Run Code Online (Sandbox Code Playgroud)
但是在这里,每次恢复命令失败时都会出现如下错误:
pg_restore: [archiver (db)] error returned by PQputCopyData: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
pg_restore: [archiver] worker process failed: exit code 1
pg_restore: [archiver (db)] error returned by PQputCopyData: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request. …Run Code Online (Sandbox Code Playgroud) 我使用PostgreSQL作为我的数据库.
我已经SCPed一个.SQL在我的远程Ubuntu虚拟机文件.
我做了sudo su - postgres并创建了一个DB.
然后我切换到我的原始帐户并尝试这个:
sudo -su postgres pg_restore < temp.sql
该命令成功运行.
但当我再次切换回postgres用户并使用\ dt检查数据库中的表列表时,我发现没有表格.
我究竟做错了什么?
我正在尝试从另一台服务器恢复使用此命令获取的pg_dump.
sudo -u postgres pg_dump --verbose --format=custom --file=pg-backup.sql -U postgres salesDB
Run Code Online (Sandbox Code Playgroud)
复制pg-backup.sql文件后,我正在尝试使用此命令进行恢复
sudo -u postgres pg_restore --verbose --jobs=`nproc` -f pg-backup.sql
Run Code Online (Sandbox Code Playgroud)
pg-backup.sql文件是13GB.pg-restore已经运行了4个小时,一直在我的屏幕上滚动数据.没有错误.
但是当我从psql会话中执行此语句时
SELECT pg_size_pretty(pg_database_size('salesDB'));
Run Code Online (Sandbox Code Playgroud)
我的大小为5377 kB.什么?它至少应该是1GB.我完全迷失了.所有这些数据都在我的屏幕上滚动,我无法证明它会在任何地方.没有磁盘使用.
救命
我在恢复表的架构时遇到一些困难。我转储了 Heroku Postgres 数据库,并使用 pg_restore 将其中一张表恢复到我的本地数据库(它有 20 多个表)。它已成功恢复,但当我尝试将新数据插入表中时遇到问题。
当我使用psql打开数据库时,我发现恢复的表包含所有数据,但其架构有零行。无论如何,我可以从转储中导入表及其架构吗?非常感谢。
这就是我将表恢复到本地数据库的方法:
pg_restore -U postgres --dbname my_db --table=message latest.dump
Run Code Online (Sandbox Code Playgroud)
编辑:
我按照官方文档尝试了类似的操作,但它被阻止了,什么也没有发生。我的数据库很小,不超过几兆字节,并且我尝试恢复的表架构不超过 100 行。
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
Run Code Online (Sandbox Code Playgroud) 我使用 pg_dump 和 pg_restore 对数据库进行每日备份,这些备份在我推送更新后最近停止工作。
我有一个函数validate_id,它是一个Case/When语句,可以快速检查一些存在完整性问题的数据。看起来像这样:
CREATE OR REPLACE FUNCTION validate_id(
_string text,
_type type
) RETURNS boolean AS
$$
SELECT
CASE WHEN (stuff) THEN TRUE
WHEN (other stuff) THEN TRUE
When (more stuff) THEN raise_err('Not an accepted type, the accepted types are: x y z')
ELSE FALSE
$$
LANGUAGE SQL;
Run Code Online (Sandbox Code Playgroud)
由于我添加了此功能,当我使用此命令转储时:
pg_dump -U postgres -h ipaddress -p 5432 -w -F t databaseName > backupsfolder/databaseName.tar
当我使用这个命令时:
pg_restore -U postgres -h localhost -p 5432 -d postgres -C …
我有个问题。有没有办法强制 PostgreSQL 9.3 备份/恢复模式以正确的依赖对象顺序?(例如:在创建物化视图本身之前,我需要创建一个在物化视图中使用的函数)。我在模式恢复时遇到错误,因为物化视图是在创建函数本身之前创建的。
谢谢
Postgres 版本:
PostgreSQL 9.2.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
Run Code Online (Sandbox Code Playgroud)
我们使用 vm 转换器将系统从一台服务器转移到另一台服务器(具有更好的 cpu 和 RAM),当我尝试备份数据库时出现错误:
pg_dump: reading schemas
pg_dump: reading user-defined tables
pg_dump: reading extensions
pg_dump: reading user-defined functions
pg_dump: reading user-defined types
pg_dump: reading procedural languages
pg_dump: reading user-defined aggregate functions
pg_dump: reading user-defined operators
pg_dump: reading user-defined operator classes
pg_dump: reading user-defined operator families
pg_dump: reading user-defined text search parsers
pg_dump: reading user-defined text search templates
pg_dump: reading user-defined text search …Run Code Online (Sandbox Code Playgroud) 问题:使用PSQLpg_dump和pg_restore在Python脚本,并使用该subprocess模块。
背景:我使用python 2.7来自本地主机(即Ubuntu 14.04.5 LTS)的以下脚本在 PSQL 服务器(即PostgreSQL 9.4.11)中创建表的备份,并将其恢复到Ubuntu 16.04.2 LTS较新版本的 PSQL 服务器(即PostgreSQL 9.6.2)中的远程主机(即)。
#!/usr/bin/python
from subprocess import PIPE,Popen
def dump_table(host_name,database_name,user_name,database_password,table_name):
command = 'pg_dump -h {0} -d {1} -U {2} -p 5432 -t public.{3} -Fc -f /tmp/table.dmp'\
.format(host_name,database_name,user_name,table_name)
p = Popen(command,shell=True,stdin=PIPE)
return p.communicate('{}\n'.format(database_password))
def restore_table(host_name,database_name,user_name,database_password):
command = 'pg_restore -h {0} -d {1} -U {2} < /tmp/table.dmp'\
.format(host_name,database_name,user_name)
p = Popen(command,shell=True,stdin=PIPE)
return p.communicate('{}\n'.format(database_password))
def …Run Code Online (Sandbox Code Playgroud) 假设我db0在本地计算机和db1远程服务器上都有。我只是想了解db1最新情况db0。
my_table假设我向in添加了几行(数千行)db0,现在我想将其插入到my_tablein中db1。
关于 pg_dump/pg_restore 的所有食谱和文档都提到了表的完全恢复,但是,我不需要(也不想)删除并my_table从头开始恢复。
是否有任何清晰且简单的方法来制作文件,scp将其发送到服务器并从中pg_restore取出文件?
只需两个命令:pg_dump 和 pg_restore。因为我花了大约两个小时查看文档和演练,发现了新的错误,并且变得越来越困惑。
我曾经pg_dump --no-privileges --format custom --compress=0 some_database > my-dump.pgdump转储过数据库,但是在尝试恢复它时遇到了问题。
具体来说,它似乎是在表定义之前加载函数定义:
$ pg_restore ./my-dump.pgdump
…
create function my_function() returns …
language sql $$
select …
from some_table
where …
$$;
… later in the dump …
create table some_table ( … );
…
Run Code Online (Sandbox Code Playgroud)
当我尝试恢复转储时会导致错误:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 4863; 0 16735 TABLE DATA some_table some_database
pg_restore: [archiver (db)] COPY failed for table "some_table": ERROR: relation "some_table" does not exist
LINE …Run Code Online (Sandbox Code Playgroud) pg-restore ×10
postgresql ×9
pg-dump ×7
disk ×1
dump ×1
heroku ×1
linux ×1
pg ×1
psql ×1
python ×1
restore ×1
space ×1
subprocess ×1