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

相关推荐
Arthas2173 分钟前
互联网大厂Java面试实战:从基础到架构的渐进式考察
java·spring boot·微服务·面试·技术栈
主角1 74 分钟前
MySQL高可用集群
数据库·mysql
dajun18112345610 分钟前
信息系统运维管理全流程详解 在线画图工具绘制运维流程图表技巧
运维·数据库·信息可视化·流程图·旅游·论文笔记
取码网12 分钟前
2025最新口红机防篡改版本源码
android·java·javascript
流觞 无依12 分钟前
SQLite数据库损坏修复指南——解决“database disk image is malformed”报错
jvm·数据库·sqlite
道清茗13 分钟前
【MySQL知识点问答题】 安全与性能管理
数据库·mysql
2501_9206276120 分钟前
Flutter 框架跨平台鸿蒙开发 - 数据库学习助手
数据库·学习·flutter·华为·harmonyos
东离与糖宝22 分钟前
2026 Java AI框架选型:Spring AI/LangChain4j企业级对比
java·人工智能
郝学胜-神的一滴23 分钟前
[力扣 20] 栈解千愁:有效括号序列的优雅实现与深度解析
java·数据结构·c++·算法·leetcode·职场和发展
代码改善世界24 分钟前
【C++初阶】手撕C++ string类
java·开发语言·c++