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

fla*_*son 12 postgresql version google-cloud-sql

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

谷歌的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
...
Run Code Online (Sandbox Code Playgroud)

我已尝试在我的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);
Run Code Online (Sandbox Code Playgroud)

Lau*_*lbe 23

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

该条款在PostgreSQL v10 中是新的,旧的服务器版本将无法理解它.AS data_typeCREATE SEQUENCE

  • 为什么没有向后兼容性转储的标志? (3认同)
  • @LaurenzAlbe v10应该知道如何转储v9.x兼容备份恕我直言 (3认同)
  • 仅作为记录,这也适用于将转储从 postgresql 10 反向移植到 portgresql **9.5**。 (2认同)

Mar*_*ndi 6

Following @"Laurenz Albe" suggestion, here's a python3 snippet can be used to downgrade a 10.x pg_dump script for 9.x:

#!/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 ')
Run Code Online (Sandbox Code Playgroud)

  • 不太复杂的解决方案是使用sed,`cat test10.sql | sed'/ AS整数/ d'&gt; test9.sql` (5认同)
  • 你真的想在任何地方删除“作为整数”吗?我的目的是在“CREATE SEQUENCE”发生后立即删除该行 (3认同)