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

相关推荐
简 洁 冬冬11 分钟前
深入理解Result与枚举的分工-外壳花名册与协作全链
java·枚举
一起学安全技术17 分钟前
Java栈Agent开发 03|Spring AI(二):Tool Calling、MCP 与结构化输出
java·人工智能·spring·spring ai·结构化输出·mcp·tool calling
果粒蹬i23 分钟前
实时计算平台是什么?工业物联网选型,DolphinDB 拆解
数据库·物联网·实时计算·dolphindb
源码_空间36 分钟前
基于Springboot的在线错题本与复习计划系统(源码+文档)
java·spring boot·后端
Rain的Java大神之路36 分钟前
别再用“延迟双删”赌一致性:Canal + RocketMQ + Redis 的工业级缓存失效链路,以及 MySQL 分库分表前必须算清的那笔账
java·后端·面试
zxcvb15336 分钟前
MySQL三种备份方式:原理、优缺点与实操选型指南
数据库·mysql
我命由我123451 小时前
Android Gradle - Android Gradle 配置
android·java·linux·java-ee·kotlin·android studio·android-studio
夜雪一千1 小时前
PostgreSQL 自定义函数入门
数据库·postgresql
himobrinehacken1 小时前
HackMyVM靶机Yulian
java·spring boot·后端
香蕉大香蕉(06敬畏市场)1 小时前
事务隔离级别、MVCC 与锁:一条线串起来
java·服务器·数据库·人工智能·面试·博客