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

相关推荐
viskaz2 分钟前
数据库迁移流程学习
数据库
auspicious航1 小时前
PostgreSQL写入慢全攻略:原因排查与性能调优实战
数据库·postgresql
凤山老林1 小时前
Spring Boot 3.x AOT 编译实战:从原理、踩坑到生产落地
java·spring boot·后端
IvorySQL1 小时前
PostgreSQL 日报|逻辑复制行过滤致数据丢失(8 月 21 日)
数据库·postgresql
博傅1 小时前
Spring 核心原理
java·后端·spring
重生之我是Java开发战士1 小时前
【Java EE】事务与事务的传播机制
数据库·java-ee
yurenpai(27届找实习中)1 小时前
从零读懂 AI 智能客服(一):模块职责与 SSE 聊天链路(后端架构)
java·人工智能·架构·langchain4j
润乾软件1 小时前
Trae+SQLazy 编写复杂 SQL 实践
数据库·sql
会编程的吕洞宾1 小时前
Redis大Key拖垮线上?排查定位与治理实战
数据库·redis·bootstrap
陈皮波比茶2 小时前
Swagger
java