【问题标题】:error during postgresql db backup restorationpostgresql db备份恢复期间的错误
【发布时间】:2020-09-25 18:45:06
【问题描述】:

在 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

但是我又得到了错误,因为表格很多,乘以四。 因此,对于每个表,我都会收到以下错误:

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 OS 有en_US.UTF-8el_GR.UTF-8。所以我想知道问题是否可能是 Windows 和 Ubuntu 字符集之间的不兼容。如果是的话,你觉得我怎么能管理它? 幸运的是,备份文件来自的 Windows 7 设备仍在使用中,因此数据库处于活动状态。但是我尝试再次创建为LC_COLLATELC_CTYPE 值与ubuntu 兼容的数据库,但没有奏效。

编辑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

但我遇到了与最初帖子中相同的错误。

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

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

然后dump新建的db,在ubuntu环境中恢复。

【问题讨论】:

    标签: postgresql pg-restore ubuntu-20.04


    【解决方案1】:

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

    locale -a  # list all locales known to OS
    

    如果您在列表中找不到语言环境,请尝试根据this post创建一个新的语言环境


    编辑 有了 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.)
    

    【讨论】:

    • 感谢您的回复。我已经在我的帖子中对此以及其他一些细节进行了“编辑”。
    • 嗯,在我当前的项目中,如果 Internet 资源需要,可以复制数据库的表,所以我寻找解决方案的意图主要与过去项目的数据库有关,这并不好迫切想出解决办法。解决方案的详细信息在我的帖子的第二次编辑中。再次感谢。
    猜你喜欢
    • 2017-02-14
    • 1970-01-01
    • 2014-06-14
    • 2011-02-17
    • 1970-01-01
    • 2013-03-31
    • 2021-08-10
    • 2014-09-03
    • 2013-12-27
    相关资源
    最近更新 更多