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

相关推荐
bksczm1 分钟前
Redis 持久化:RDB 快照与 AOF 日志
数据库·redis·mybatis
SimonKing22 分钟前
震惊:怎么回事,opencode一轮对话耗尽免费额度
java·后端·程序员
装不满的克莱因瓶25 分钟前
【SpringAI Alibaba】ReactAgent响应SSE实时流式传输案例实战
java·人工智能·ai·sse·springai
.痞老板.27 分钟前
mysql初阶--数据库设计
数据库·mysql·oracle
杨凯凡29 分钟前
Spring Boot 文件上传怎么做才安全?流式落盘、路径校验与数据库一致性实战
java·spring boot·文件上传·后端安全·数据库一致性
这个昵称不容易1 小时前
旅游管理系统的设计与实现(SpringBoot + MySQL + Vue 前后端分离)
java·springboot·毕设
Rain的Java大神之路1 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
Decay__2 小时前
SQL Server 数据库全栈运维实战系列 — 合集索引(24 篇)
运维·数据库·sql·mysql·架构
天天被压力2 小时前
【A股分钟级高频数据完全手册 #11】券商实时行情数据接口:实时交易(券商源)接入与字段
java·人工智能·python
Thanks_ks2 小时前
SQL 的 GROUP BY 与聚合函数:从会写 SELECT 到会做统计
数据库·sql·mysql·group by·数据统计·聚合函数·having