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

相关推荐
摇滚侠25 分钟前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
飞花漫卷,情随岁迁34 分钟前
网络数据包转发与内核机制:小白概念科普
linux·运维·网络·数据库·mysql·oracle·智能路由器
wangxiaoxuan2171 小时前
大模型怎样调用机器人能力:目标落地、工具调用与执行反馈
数据库·机器人·php
凌冰_2 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
Rain的Java大神之路4 小时前
Spring AI 2.0 GA 深度复盘:Java 后端做 AI 的分水岭,到底画在哪?
java·后端·aigc
数据世界14 小时前
从立项选品到竞争情报:医药人查询国内品种格局数据的难点与5.0版结构化整理
大数据·数据库·人工智能
如夏倾城5 小时前
进程状态模型详解:从三态、五态到 Linux 状态码映射
java·linux·服务器
小南家的青蛙6 小时前
JVM 参数调优实战指南
java·jvm
AAA修煤气灶刘哥7 小时前
数据搬到另一朵云,小红书的训练任务怎么接着读?
数据库·后端·架构