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

相关推荐
云恒要逆袭2 分钟前
Hello World背后的秘密:Java程序是这样运行的
java·后端·程序员
JAVA9653 分钟前
JAVA面试-并发篇 09-LockSupport 和 waitnotify 的区别
java·开发语言·面试
蝎子莱莱爱打怪6 分钟前
XZLL-IM干货系列 01|万字拆解分布式 IM 架构:7 个微服务 + 自研 Flutter SDK
java·后端·面试
计算机安禾6 分钟前
【数据库系统原理】第9篇:SQL的结构化思维:DDL、DML与DCL的职责分离
数据库·sql·oracle
计算机安禾10 分钟前
【数据库系统原理】第12篇:视图机制:外模式在SQL层级的逻辑数据独立性实现
数据库·sql·oracle
程序员小羊!13 分钟前
07Java IO 流
java·开发语言
ZC跨境爬虫13 分钟前
跟着 MDN 学JavaScript day_10:数组——数据的有序集合
android·java·开发语言·前端·javascript
前进的李工14 分钟前
MySQL性能优化:索引与子查询实战技巧
数据库·sql·mysql·性能优化
亦暖筑序17 分钟前
Java 8老系统旁路接入AI Gateway:不升级JDK也能用AI
java·spring boot·aigc·企业架构·ai gateway
IT龟苓膏20 分钟前
Java 集合进阶:ConcurrentHashMap、HashSet、LinkedHashMap、TreeMap 和 fail-fast 一篇讲清
java·开发语言·jvm