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

相关推荐
学长毕设4 分钟前
基于SpringBoot的烘焙商品商品系统-附源码
java·spring boot·后端
updayday85423 分钟前
客户附件不该发往旧邮箱,如何在发送前拦截 - Ping64 邮件安全管控
大数据·网络·数据库·安全·智能路由器
倔强的石头_29 分钟前
连接异常排障手册:拒连、超时、认证失败、空闲断开
数据库
金銀銅鐵44 分钟前
[Java] 用纯 Swing 做一个《論語·學而》阅读器
java·ai编程·交互设计
lhbweilai1 小时前
QT(了解各个控件和布局)上
数据库·qt
莫晓燚1 小时前
搭建Redis 主从复制---实战
数据库·redis·缓存
艾伦_耶格宇1 小时前
【kubernetes - 1.28版本】-11 K8S核心组件
java·容器·kubernetes
灵晔君2 小时前
【MySQL】数据库基础
数据库·mysql·adb
2301_780789662 小时前
深度伪造即服务:新型网络诈骗的“完美面具”与识别之道
java·网络·ddos
LHICOD2 小时前
Linux-2
java·linux·后端·jar·vmware·svm·vm