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

相关推荐
2401_8914092617 分钟前
期权全量合约历史数据:分钟/Tick/日级/L2五档字段清单
数据库
海宇服务23 分钟前
零信任架构实战:基于海宇企业司法涉诉构建自动化分包商准入合规审查网关
数据库·人工智能·架构·自动化
Nazarick30 分钟前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
黑色的白兔No11 小时前
若依平台cloud版本搭建
java·spring cloud·微服务·nacos·vue·若依
W_son1 小时前
数据库归档模式开启与备份作业配置实战
数据库·oracle·达梦数据库
郝学胜-神的一滴1 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
syf0611131 小时前
【Java】synchronized 关键字详解:从字节码到对象头与锁升级
java·开发语言
其实防守也摸鱼2 小时前
Day 3 · Agent Loop:从调用模型到控制循环 —— 课件精讲笔记
服务器·开发语言·javascript·数据库·人工智能·笔记·ecmascript
商业看点解说2 小时前
八个选型要点:企业搭建外部工具调用型AI智能体的模型评估指南
数据库
许彰午2 小时前
100-Oracle学习路线从入门到精通
数据库·oracle