【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)

相关推荐
LMT流氓兔几秒前
第四单元——第17课:让回答边生成、边显示
服务器·数据库·人工智能·python·oracle·langchain
北风toto几秒前
Linux 服务器如何判断 CPU 是否支持 x86-64-v2 指令集
java·linux·服务器
1024奇点2 分钟前
【仓颉语言入门 · 第二篇 第 8 课】
开发语言·数据库·ide·mysql·开源
萧瑟余晖4 分钟前
Spring Security综合实战与安全加固详解
java·安全·spring
imDwAaY18 分钟前
操作系统如何管理程序运行?从线程、进程到内存隔离与用户态切换:CS162 Note2
android·java·redis
其实防守也摸鱼21 分钟前
Linux文本处理命令理论学习
linux·服务器·前端·数据库·sql·学习·安全
const_qiu23 分钟前
第三章 08:3.10 本章小结
java·人工智能
Pniubi24 分钟前
快速排序(力扣基础)(递归)
java·数据结构·算法
CarIise41 分钟前
Spring MVC 请求参数绑定与常用注解2
java·spring·mvc
l1t1 小时前
DuckDB 各版本 tpch 10 查询时间对比
数据库