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

相关推荐
金銀銅鐵3 小时前
类实例里还有什么——putfield、getfield 与对象传递
java·jvm·后端
hryou09223 小时前
向量库选型别踩坑:10个候选、6个暗坑,一次说清
数据库·架构
emoji1111113 小时前
python总结
数据库
默 语3 小时前
HarmonyOS7 事件分发控制(二):onTouchIntercept 镂空玻璃卡——圆内自己接,圆外放行
android·java·开发语言
zxcvb1535 小时前
运维实践里把备份mysql数据库做稳的一些思路
运维·数据库·mysql
sharemylove5 小时前
Java 异常与常用类 学习笔记
java·开发语言·经验分享·笔记·学习
炸薯条!7 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
Dreams_l7 小时前
基于SpringBoot实现的抽奖系统(四)
java·spring boot·后端
一级新生7 小时前
Dify 迭代节点:从输入输出到九步实操
数据库·人工智能·mysql·低代码·工作流·dify
这个昵称不容易7 小时前
SpringBoot 网上订餐系统设计与实现:80 个 Java 文件、91 个接口,附真实运行截图
java·springboot·毕设