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

相关推荐
tachibana24 分钟前
知识库文档上传接口
数据库·人工智能·大模型·llm
可乐ea11 分钟前
Tool Calling 工具调用:让 Agent 查询数据库、调用接口和执行任务
数据库·prompt·agent·tool
未秃头的程序猿27 分钟前
凌晨3点被叫醒:线上OOM,我用这套流程40分钟定位根因
java·jvm·后端
Yan_chen66627 分钟前
CTFHub SQL 布尔盲注实战攻略
数据库·sql·实战·布尔盲注·ctfhub闯关
letisgo544 分钟前
JDK21 + Spring AI 2.0 入场指南:2026 年 Java 开发者 AI 架构全景图
java·ai·idea·jdk21
白仑色1 小时前
Spring Boot 从切面统一控制事务
java·spring boot·aop·spring事务
杨_晨1 小时前
LLM输出康熙部首冲突
数据库·python·mysql·ai
Flynt1 小时前
Java并行流,让我debug了一整天
java
爱看报的猿1 小时前
【金仓数据库征文】MySQL至金仓KES异构数据库平滑迁移与性能深度调优实战
数据库·数据仓库·mysql·金仓数据库征文
DFT计算杂谈1 小时前
FeSe超薄膜在CaF2衬底上的电子结构DFT研究
java·服务器·前端