postgresql 数据库备份恢复期间出错

error during postgresql db backup restoration

在 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

一切正常。

然而,当我试图在 Ubuntu 20.04 中在不同的设备上恢复它时,我收到了一个错误: could not execute query: ERROR: invalid locale name:(与here相同)

所以我按照给定的说明创建数据库,

sudo -u postgres psql
create database [db_name];

然后我在终端中输入以下命令来恢复备份:

pg_restore -U postgres -d postgres /home/../../[db_backup_file].sql

但是我又犯了错误,因为 table 乘以四。 所以对于每个 table 我都会得到以下错误:

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]);

创建 table 时,主键 - 如果与它有关 - 被定义为自动递增,形式为:

CREATE TABLE [table_name] (
    [pkey_column_name] serial primary key,
    .......
    .......
    .......
    .......
    .......
    .......
);

谁能帮我解决这个问题?

编辑:实际上我昨天 post 的第一个错误中缺少的代码页类型是 "Greek_Greece.1253"。我按照你说的使用了 locale -a 命令,我看到我的 Ubuntu OS 有 en_US.UTF-8el_GR.UTF-8。所以我想知道问题是否可能是 Windows 和 Ubuntu 字符集之间的不兼容。如果是的话,你认为我如何处理它? 幸运的是,备份文件来自的 windows 7 设备仍在使用中,因此数据库处于活动状态。但是我试图再次创建与 ubuntu 兼容的 LC_COLLATELC_CTYPE 值的数据库没有用。

编辑 2:最后是 windows-linux 字符编码不兼容。 当我尝试将 en_US.UTF-8el_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

然后我尝试在 ubuntu 中创建数据库,然后再恢复它,命令如下:

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

但我遇到了与最初 post.

相同的一批错误

所以解决方案是在 windows 中创建一个新数据库,但现在在 'C' 字符编码下(POSIX 不会被接受),复制 tables 从一个数据库到另一个数据库:

pg_dump -U postgres -t [table_name] [database_name] | psql -U postgres -d [database_name]

然后转储新创建的数据库,并在ubuntu环境中恢复它。

可能是您的 Ubuntu OS 没有 en_US.UTF-8 语言环境。您可以在终端中使用以下命令来检查:

locale -a  # list all locales known to OS

如果您在列表中找不到语言环境,请尝试根据

重新创建一个

编辑 有了 Windows 编码是 Greek_Greece.1253 的附加信息,听起来仍然不匹配。根据 pg_dump docs,您可以使用 -E 选项显式设置编码。可能您想将其设置为 Ubuntu 可以处理的东西(即 en_US.UTF-8el_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.)