【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 分钟前
基于ssm的技能比赛报名管理系统29817vn0(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
java·数据库·spring·ssm 框架应用
小小码农Come on3 小时前
Qt Creator + MSVC 2022 64bit 配置 Dump 文件生成与分析流程
数据库·qt
代码雕刻家3 小时前
3.5.Maven-依赖管理-依赖配置&依赖传递
java·maven
qiuyuyiyang3 小时前
【MySQL】环境变量配置
数据库·mysql·adb
!chen3 小时前
MyBatis-plus拓展之字段类型处理器、自动填充和乐观锁
java·tomcat·mybatis
Jin、yz4 小时前
JAVA 八股
java·开发语言
va学弟4 小时前
Java 网络通信编程(6):视频通话
java·服务器·网络·音视频
pjw198809034 小时前
Spring Framework 中文官方文档
java·后端·spring
jgyzl4 小时前
2026.3.11MyBatis-Plus基本使用与思考
java·数据库·mybatis
RDCJM5 小时前
【MySQL】在MySQL中STR_TO_DATE()以及其他用于日期和时间的转换
android·数据库·mysql