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

相关推荐
步行cgn几秒前
Spring 注入值中含有特殊符号的处理详解
java·后端·spring
user_admin_god3 分钟前
第 11 篇:实践三 —— 表单 / 合同字段抽取
java·人工智能·spring boot·语言模型
未秃头的程序猿3 分钟前
全链路追踪落地一年:从翻日志到秒级定位,我们都做了什么
java·后端·面试
用户7531057124514 分钟前
五年前我吐槽 PG 的 32 位 XID 是狗皮膏药,今天它被治好了
数据库
DBA_G8 分钟前
从湖仓一体到AI原生:GBase数据库的金融全栈技术实践
数据库
SamDeepThinking10 分钟前
new Thread()之后发生了什么?
java·后端·面试
斯维赤17 分钟前
Spring AI | Function Calling 是什么?
java·后端
devpotato17 分钟前
限流、熔断、降级:三者核心区别一文讲清
java
不灭的黄金瞳12317 分钟前
Java数据类型与变量
java·开发语言·intellij-idea
ChenLuck18 分钟前
32 位 XID 的“狗皮膏药”被撕掉了:金仓 V9 的 64 位事务号实测与底层拆解
数据库