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

相关推荐
人活一口气9 分钟前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc
SelectDB32 分钟前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
这个DBA有点耶2 小时前
GROUP BY优化全解:如何写出既不丢数据又飞快的分组查询
数据库·mysql·架构
像我这样帅的人丶你还2 小时前
Java 后端详解(三):全局异常处理与 JPA 数据库映射
java·后端
NE_STOP2 小时前
vibe Coding -- 小项目实战
java
掉头发的王富贵5 小时前
【StarRocks】极限十分钟入门StarRocks
数据库·sql·mysql
Nturmoils5 小时前
WHERE 条件别凭习惯写,常用查询先跑一遍
数据库
未秃头的程序猿8 小时前
Java 26正式发布!这3个新特性,让代码量直接减半
java·后端·面试
用户298698530149 小时前
Word 文档文本查找与替换的 Java 实现方案
java·后端
阿哉9 小时前
Nacos 服务发现源码:藏在背后的两套事件机制,90%的人只讲了一半
java