【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://[email protected]: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)

相关推荐
趁你还年轻_28 分钟前
Redis大量key集中过期怎么办
数据库·redis·缓存
我爱Jack38 分钟前
ObjectMapper 在 Spring 统一响应处理中的作用详解
java·开发语言
GUIQU.42 分钟前
【Oracle】视图
数据库·oracle
南棱笑笑生43 分钟前
20250605在微星X99主板中配置WIN10和ubuntu22.04.6双系统启动的引导设置
数据库
捡田螺的小男孩1 小时前
京东一面:接口性能优化,有哪些经验和手段
java·后端·面试
小白杨树树1 小时前
【SSM】SpringMVC学习笔记8:拦截器
java·开发语言
Leo.yuan1 小时前
实时数据仓库是什么?数据仓库设计怎么做?
大数据·数据库·数据仓库·数据分析·spark
艾露z1 小时前
深度解析Mysql中MVCC的工作机制
java·数据库·后端·mysql
冷心笑看丽美人1 小时前
Spring MVC 之 异常处理
java·开发语言·java-ee·spring mvc