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

相关推荐
PaperData5 分钟前
1985-2025年全国区县专利申请与授权面板数据
数据库
snpgroupcn7 分钟前
大数据量SAP迁移方案:系统越大,越不能硬搬
数据库·sap
leikooo26 分钟前
用餐厅和盘子理解栈和堆
java·数据结构·链表·rust
Flynt1 小时前
Redis Cluster主节点挂了,为什么"高可用"还全员掉线?我把三次kill的记录翻出来了
数据库·redis·分布式
笃行3501 小时前
KingbaseES 数据加密全解:从 SSL 到全密态
数据库
Ivanqhz2 小时前
激活函数在 Transformer 中的作用及各种变体简述
java·linux·数据库·人工智能·深度学习
沙漠之主2 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
FPGA小徐2 小时前
PA1 基础设施(2)深度解析:NEMU 中 ITRACE、MTRACE、FTRACE、IRINGBUF、ETRACE、DIFFTEST 全解析
java·服务器·网络
YOLO数据集集合3 小时前
EvoAgent:面向PR研发治理的自进化Multi-Agent Harness系统
java·开发语言·目标检测·agent·自进化
java1234_小锋3 小时前
【技术专题】Mysql8 数据库 - Mysql8 数据类型简介
数据库·mysql