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

相关推荐
下一站春风里42 分钟前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
挖掘狂人1 小时前
用户连点两次「确认支付」,你的后端凭什么只扣一次钱?
java·后端·面试
codigger1 小时前
用户连点两次「确认支付」,你的后端凭什么只扣一次钱?
java·程序员·支付·java后端
Resean02231 小时前
SpringMVC 6+源码分析(十)数据绑定与类型转换
java·spring·springmvc
lw_zhang11011 小时前
爆肝一个月,Java 从入门到精通39 - 使用Log4j
java·单元测试·log4j
Rain的Java大神之路1 小时前
面试官问"MySQL 大表 COUNT(*) 怎么优化",90% 的人第一步就答错了
java·后端·面试
唐老板1 小时前
AI 都会编程了,Java 何去何从
java·ai编程
nice_lcj5201 小时前
MySQL 慢查询与 EXPLAIN:怎么定位、怎么优化
java·数据库·后端·mysql
芜名氏2 小时前
API 文档生成工具——SpringDoc
java·spring boot·ci/cd·设计规范
这个昵称不容易3 小时前
SpringBoot 医疗病历交互系统毕设:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设