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

相关推荐
CC.GG20 分钟前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
Overt0p1 小时前
抽奖系统(4)
java·spring boot·tomcat
编程小Y1 小时前
如何优化MySQL的查询性能?
数据库·mysql
用户47949283569151 小时前
性能提升 40 倍!实战 PostgreSQL FDW 解决微服务跨库查询难题
数据库·后端
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue宠物医院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
贺今宵2 小时前
安装better-sqlite3报错electron-vite
javascript·sql·sqlite·sqlite3
kimi-2222 小时前
LangChain 将数据加载到 Chroma 向量数据库
数据库·langchain
想做后端的小C2 小时前
Java:接口回调
java·开发语言·接口回调
爱学习的小可爱卢2 小时前
JavaEE进阶——Spring核心设计模式深度剖析
java·spring·设计模式
毕设源码-钟学长3 小时前
【开题答辩全过程】以 个性化电影推荐网站的设计与实现为例,包含答辩的问题和答案
java·spring boot