无法将PostgreSQL10转储导入9.6数据库

时间:2018-03-29 03:35:50

标签: postgresql version google-cloud-sql

我需要以某种方式将v10转储文件转换为兼容9.6的

Google的Cloud SQL运行PostgreSQL版本9.6,我的数据库自创建以来一直在版本10上运行。

问题:尝试将数据库导入Cloud SQL时,我收到an unknown error has occurred.死亡消息。

在导入到Cloud SQL时,我已尝试评论我的postgis /其他扩展程序,但无济于事。

我尝试过使用psql my_96_db < my_10.sql并遇到大量错误:

...
CREATE TABLE
ERROR:  syntax error at or near "AS"
LINE 2:     AS integer
            ^
ERROR:  relation "authentication_phonecontact_id_seq" does not exist
CREATE TABLE
...

我尝试在我的v10 pg_dump -Fc命令中使用postgres 9.6的pg_restore,但它无法成功导入9.6数据库。输出中许多失败之一的一个例子是

pg_restore: [archiver (db)] could not execute query: ERROR:  relation "public.authentication_referral_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.authentication_referral_id_...
                                 ^
    Command was: SELECT pg_catalog.setval('public.authentication_referral_id_seq', 1, false);

2 个答案:

答案 0 :(得分:19)

根据您显示的错误消息判断,您必须编辑SQL转储并从所有AS integer语句中删除所有出现的CREATE SEQUENCE

AS data_type CREATE SEQUENCE子句是PostgreSQL v10中的新增内容,而较旧的服务器版本将无法理解它。

答案 1 :(得分:3)

根据@“ Laurenz Albe”的建议,以下是一个python3代码段,可用于将9.x的10.x pg_dump脚本降级:

#!/usr/bin/env python3
import sys

#
#  Downgrades pg_dump 10 script to 9.x
#  removing 'AS integer' from 'CREATE SEQUENCE' statement
#
#  Usage:
#       $ python3 pgdump_10_to_9.py < test10.sql > test9.sql
#  or:
#       $ cat test10.sql | ./pgdump_10_to_9.py > test9.sql
#
#  To obtain a compressed 9.x sql script from a compressed 10 sql script:
#
#       $ gunzip -c test10.sql.gz | ./pgdump_10_to_9.py | gzip > test9.sql.gz
#

inside_create_sequence = False
for row in sys.stdin.readlines():

    if inside_create_sequence and row.strip().lower() == 'as integer':
        pass
    else:
        print(row, end='', flush=True)

    inside_create_sequence = row.strip().startswith('CREATE SEQUENCE ')
相关问题