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

相关推荐
JasmineWr5 分钟前
CompletableFuture相关问题
java·开发语言
零雲7 分钟前
java面试:知道java的反射机制吗
java·开发语言·面试
Psycho_MrZhang9 分钟前
Redis 设计思想总结
数据库·redis·缓存
java1234_小锋1 小时前
Java进程占用的内存有哪些部分?
java
sxlishaobin1 小时前
Spring Bean生命周期详解
java·后端·spring
曹牧1 小时前
Java:Assert.isTrue()
java·前端·数据库
梦里小白龙1 小时前
JAVA 策略模式+工厂模式
java·开发语言·策略模式
你不是我我2 小时前
【Java 开发日记】我们来说一说 Redis 主从复制的原理及作用
java·redis·github
程序员葫芦娃2 小时前
【Java毕设项目】基于SSM的旅游资源网站
java·开发语言·数据库·编程·课程设计·旅游·毕设
Pocker_Spades_A2 小时前
飞算Java在线学生成绩综合统计分析系统的设计与实现
java·开发语言·java开发·飞算javaai炫技赛