使用pgloader迁移数据至PostgreSQL,如何指定非public schema?
解决pgloader将SQLite数据导入PostgreSQL指定Schema的问题
pgloader本身支持直接指定目标Schema,无需事后用ALTER SCHEMA调整,以下是两种可行方案:
方案一:在PostgreSQL连接URL中指定搜索路径
修改INTO行的连接串,通过options参数设置search_path为目标Schema(注意URL编码,%3D是等号的转义):
LOAD DATABASE FROM sqlite:///Users/downloads/Downloads/SQLite_ENTIRE_test.db INTO postgresql://postgres:postgres@localhost/Test?options=-csearch_path%3Dyour_target_schema WITH include drop, create tables, create indexes, reset sequences, prefetch rows = 1000;
方案二:在配置脚本中添加SET SCHEMA指令
在WITH块之前插入SET SCHEMA语句,明确指定目标Schema:
LOAD DATABASE FROM sqlite:///Users/downloads/Downloads/SQLite_ENTIRE_test.db INTO postgresql://postgres:postgres@localhost/Test SET SCHEMA 'your_target_schema' WITH include drop, create tables, create indexes, reset sequences, prefetch rows = 1000;
前置步骤:确保目标Schema已存在
执行PostgreSQL命令创建目标Schema(如果尚未存在):
CREATE SCHEMA IF NOT EXISTS your_target_schema;
关于ALTER SCHEMA无效的说明
如果事后执行ALTER SCHEMA移动表未生效,大概率是以下原因:
- 执行命令时未指定正确的表名(需用
ALTER TABLE public.table_name SET SCHEMA your_target_schema;逐个移动) - 当前用户没有操作目标Schema的权限
- 目标Schema中已存在同名表
内容的提问来源于stack exchange,提问作者KoolKid
相关产品推荐
相关产品推荐

