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

相关推荐
于先生吖几秒前
前后端分离开发 Java 跑腿系统:用户 + 骑手 + 后台三端实战
java·开发语言
云烟成雨TD1 分钟前
Spring AI Alibaba 1.x 系列【2】架构、特性与生产级演示案例
java·人工智能·spring
iPadiPhone2 分钟前
Java NIO 核心原理解析、性能调优与大厂面试精要
java·后端·面试·nio
皙然3 分钟前
深度解析三色标记算法:JVM 并发 GC 的核心底层逻辑
java·jvm·算法
大写的老王3 分钟前
OpenClaw 部署实战:一周完成 PHP 到 Java 的项目迁移
java·php·ai编程
LF3_4 分钟前
监听数据库binlog日志变化,将变动实时发送到kafka
数据库·分布式·mysql·kafka·binlog·debezium
我真会写代码6 分钟前
从入门到精通:Redis实战指南,解锁高性能缓存核心能力
数据库·redis·缓存
hnlgzb6 分钟前
Gemini:kotlin这几个类型有什么区别?类比java的文件,是怎样的?
java·开发语言·kotlin
温酒斟与你7 分钟前
idea编辑器新版UI回归旧版
java·ide·intellij-idea
God__is__a__girl8 分钟前
IntelliJ IDEA 启动失败问题解决记录
java·ide·intellij-idea