当我尝试恢复 PostgreSQL 转储时出现很多“无效命令 \N”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31793393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:59:51  来源:igfitidea点击:

A lot of "invalid command \N" when I try to restore PostgreSQL dump

postgresqldumpdatabase-restorepg-dump

提问by Andrey

I made a backup of a database on my mac and tried to restore it on a computer with ubuntu. When I execute

我在我的 mac 上备份了一个数据库,并尝试在带有 ubuntu 的计算机上恢复它。当我执行

psql -U uname -d dbname -f ~/dump_from_mac

I have a lot of error messages like "invalid command \N" and "relation 'SomeTable' does not exist". My question is very similar with Can't copy table to another database with pg_dumpbut I don't know how to fix my dump file. I wipe my mac and can't make new dump.

我有很多错误消息,例如“无效命令 \N”和“关系 'SomeTable' 不存在”。我的问题与Can't copy table to another database with pg_dump非常相似, 但我不知道如何修复我的转储文件。我擦拭了我的 mac 并且无法创建新的转储。

采纳答案by Andrey

My problem was solved by setting postgresql-contrib package

我的问题是通过设置 postgresql-contrib 包解决的

sudo apt-get install postgresql-contrib

and creating extension uuid-ossp in my db

并在我的数据库中创建扩展 uuid-ossp

CREATE EXTENSION "uuid-ossp";

My db haven't this extension by default and psql could not execute uuid_generate_v1() function from my dump file. In most cases install postgresql-contrib is enough, but sometimes problem may be in some missed extensions too.

我的数据库默认没有这个扩展名,psql 无法从我的转储文件中执行 uuid_generate_v1() 函数。在大多数情况下,安装 postgresql-contrib 就足够了,但有时问题也可能出在某些错过的扩展中。

回答by Ekrem Gurdal

Here is my solution:

这是我的解决方案:

1) pg_dump with inserts:

1) 带插入的 pg_dump:

pg_dump dbname --username=usernamehere --password --no-owner --no-privileges --data-only --inserts -t 'schema."Table"' > filename.sql

2) psql (restore your dumped file)

2) psql (恢复你的转储文件)

psql "dbname=dbnamehere options=--search_path=schemaname" --host hostnamehere --username=usernamehere -f filename.sql >& outputfile.txt

Note-1 ) Make sure that adding outputfile will increase speed of import.

Note-1 ) 确保添加 outputfile 将提高导入速度。

Note-2 ) Do not forget to create table with exact same name and columns before importing with psql.

Note-2 ) 在使用 psql 导入之前,不要忘记创建具有完全相同名称和列的表。