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

相关推荐
2501_9339232517 分钟前
聚合查询与分组实战:从列筛选到行计算
数据库·mysql
用户0723670666018 分钟前
接口级配 50%、方法级配 30%,这个方法实际切多少
java·dubbo
淘源码A22 分钟前
智能预问诊系统源码,基于SpringBoot、MyBatis-Plus、uni-app等技术栈,开箱即用
java·spring boot·源码·二次开发·电子病历·预问诊·智能预问诊
夏幻灵26 分钟前
Java 学习日记:多态
java·开发语言·学习
雾隐隐o28 分钟前
AI 零代码生成平台 06|对话历史、游标分页与 Redis 记忆
数据库·人工智能·redis
apcipot_rain30 分钟前
C++转Java日记(19)
java·开发语言
富贵儿99840 分钟前
NL2SQL 实践手记 09:答案正确以后,还能更快吗——一次可核对的 AI4DB 实验
数据库·python·sql·ai·自然语言处理·langchain
JAVA面经实录91741 分钟前
AI 辅助写单元测试
java·人工智能·单元测试·ai编程
IT枫斗者1 小时前
浏览器跨域预检 OPTIONS:什么时候触发,为什么被接口拒绝
java·开发语言·数据库·mysql·fastapi
SimonKing1 小时前
可以自动部署项目的SSH客户端,部署时间可以摸鱼了
java·后端·程序员