在Windows 7中创建的数据库备份文件:
pg_dump -U postgres -Fc [db_name] >D:\[db_backup_file].sql
然后,我删除了它,并恢复它来测试这个过程:
pg_restore -U postgres -C -d postgres D:\[db_backup_file].sql
一切都很好。
然而,当我试图在另一个设备中恢复Ubuntu20.04时,我得到了一个错误:could not execute query: ERROR: invalid locale name:
(与这里相同)
所以我按照给定的指令创建了数据库,
sudo -u postgres psql
create database [db_name];
然后,我在终端中放置了以下命令来恢复备份:
pg_restore -U postgres -d postgres /home/../../[db_backup_file].sql
但是,我又一次犯了错误,因为很多是表格,乘以4。因此,对于每个表,我都会得到以下错误:
pg_restore: from TOC entry 315; 1259 29971 TABLE [table_name] postgres
pg_restore: error: could not execute query: ERROR: relation [table_name] already exists
Command was: CREATE TABLE public.[table_name] (
[pkey_column_name] integer NOT NULL,
.......
.......
.......
.......
.......
.......
);
pg_restore: from TOC entry 314; 1259 29969 SEQUENCE [table_name]_[pkey_column_name]_seq postgres
pg_restore: error: could not execute query: ERROR: relation "[table_name]_[pkey_column_name]_seq" already
exists
Command was: CREATE SEQUENCE public.[table_name]_[pkey_column_name]_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
pg_restore: from TOC entry 3522; 0 29971 TABLE DATA [table_name] postgres
pg_restore: error: COPY failed for table "[table_name]": ERROR: duplicate key value violates unique constraint "[table_name]_pkey"
DETAIL: Key ([pkey_column_name])=(1) already exists.
CONTEXT: COPY [table_name], line 1
pg_restore: from TOC entry 3267; 2606 29976 CONSTRAINT [table_name] [table_name]_pkey postgres
pg_restore: error:
could not execute query: ERROR: multiple primary keys for table "[table_name]" are not allowed
Command was: ALTER TABLE ONLY public.[table_name]
ADD CONSTRAINT [table_name]_pkey PRIMARY KEY ([pkey_column_name]);
在创建表时,主键(如果与其有关)被定义为自动增量,形式为:
CREATE TABLE [table_name] (
[pkey_column_name] serial primary key,
.......
.......
.......
.......
.......
.......
);
有人能帮我一下吗?
编辑:实际上,我昨天发布的第一个错误中缺少的代码页类型是"Greek_Greece.1253"
。正如您所说的,我使用了locale -a
命令,并且我看到了我的Ubuntu中有en_US.UTF-8
和el_GR.UTF-8
。所以我想知道问题是否可能是Windows和Ubuntu字符集之间的不兼容。如果是的话,你觉得我能应付得了吗?幸运的是,备份文件来自的windows 7设备仍在使用中,因此数据库处于活动状态。但是,我试图再次创建与ubuntu兼容的LC_COLLATE
和LC_CTYPE
值的数据库是行不通的。
编辑2:最后是windows-linux在字符编码中的不兼容性。当我尝试在编码参数中使用en_US.UTF-8
或el_GR.UTF-8
时,如下所示:
pg_dump -E en_US.UTF-8 -U postgres -Fc [db_name] > D:\[backup_file].sql
我得到了:
pg_dump: invalid client encoding "en_US.UTF-8" specified
然后,在还原db之前,我尝试在ubuntu中创建db,命令如下:
CREATE DATABASE database_name WITH ENCODING 'utf8' LC_COLLATE='el_GR.utf8' LC_CTYPE='el_GR.utf8' TEMPLATE template0;
然后:
pg_restore -U postgres -d postgres ~/../../backup_file.sql
但我得到了同样的错误,我在最初的帖子。
因此,解决方案是在windows中创建一个新数据库,但现在在“C”字符编码(POSIX不被接受)下,将表从一个数据库复制到另一个数据库:
pg_dump -U postgres -t [table_name] [database_name] | psql -U postgres -d [database_name]
然后转储新创建的db,并在ubuntu环境中恢复它。
发布于 2020-06-06 17:57:19
可能是您的Ubuntu没有en_US.UTF-8
语言环境。您可以在终端中使用以下命令来检查这一点:
locale -a # list all locales known to OS
如果无法在列表中找到区域设置,请尝试根据这个职位创建一个新的区域
使用Windows编码是Greek_Greece.1253
的附加信息编辑,听起来仍然存在不匹配。根据转储文档,可以使用-E选项显式设置编码。您可能希望将其设置为Ubuntu可以处理的东西(即en_US.UTF-8
或el_GR.UTF-8
)。
-E encoding
--encoding=encoding
Create the dump in the specified character set encoding. By default, the dump is
created in the database encoding. (Another way to get the same result is to set the
PGCLIENTENCODING environment variable to the desired dump encoding.)
https://stackoverflow.com/questions/62234333
复制相似问题