【postgres】sqlite格式如何导入postgres数据库

step1 在ubuntu系统安装pgloader(centos系统难以直接通过yum安装,如果源码安装的话,会比较费劲)

step2,执行如下python脚本

from pathlib import Path

import subprocess

dataset_dir = Path('/app/sqlite_to_pg/chase-page/data/database/')

遍历目录获取所有文件路径

file_paths = str(p.absolute()) for p in dataset_dir.rglob('\*') if p.is_file()

for fn in file_paths:

创建或更新 a.load 文件内容

load_file_content = f"""

LOAD DATABASE

FROM sqlite://{fn}

INTO pgsql://postgres@192.168.1.14:5432/tablegpt_test_chase

WITH include drop, -- 在导入前删除已存在的表

create tables, -- 自动创建目标表

create indexes, -- 创建索引

reset sequences -- 重置序列

CAST

type number to text,

type time to text,

type string to text;

"""

写入 a.load 文件

with open('a.load', 'w') as file:

file.write(load_file_content)

执行 pgloader 命令

command = 'pgloader', 'a.load'

try:

result = subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

print("Command executed successfully")

print("Output:", result.stdout)

except subprocess.CalledProcessError as e:

print("An error occurred while executing the command:")

print("Exit status:", e.returncode)

print("Error output:", e.stderr)

相关推荐
用户298698530141 小时前
Java 实现 Word 文档加密与权限解除
java·后端
Yeats_Liao2 小时前
14:Servlet中的页面跳转-Java Web
java·后端·架构
未秃头的程序猿2 小时前
告别"if-else地狱"!Java 21模式匹配,代码优雅了10倍
java·后端·面试
鹤望兰6752 小时前
字节跳动国际支付-后端开发-三面面经
java
Flittly3 小时前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring
RainCity3 小时前
Java Swing 自定义组件库分享(十二)
java·笔记·后端
xiezhr4 小时前
逛GitHub发现了一款免费的带AI功能的数据库管理工具
数据库·ai编程·dba
吃饱了得干活18 小时前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
lwx5728020 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
Flynt1 天前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端