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

相关推荐
蓝倾9761 分钟前
唯品会以图搜图(拍立淘)API接口调用指南详解
java·大数据·前端·数据库·开放api接口
考虑考虑6 分钟前
JDK17随机数生成
java·后端·java ee
Ai财富密码10 分钟前
【Python爬虫】正则表达式入门及在数据提取中的高效应用
数据库·mysql·php
{⌐■_■}11 分钟前
【MongoDB】索引分类,使用案例讲解,explain三种模式讲解,及性能排查案例
数据库·mongodb
D_alyoo21 分钟前
Activiti 中各种 startProcessInstance 接口之间的区别
java·activiti
斯普信专业组21 分钟前
k8s调度问题
java·容器·kubernetes
Olrookie29 分钟前
若依前后端分离版学习笔记(七)—— Mybatis,分页,数据源的配置及使用
数据库·笔记·学习·mybatis·ruoyi
Hx__31 分钟前
Redis中String数据结构为什么以长度44为embstr和raw实现的分界线?
数据结构·数据库·redis
慕y27436 分钟前
Java学习第一百一十一部分——Jenkins(二)
java·开发语言·学习·jenkins
草履虫建模42 分钟前
RuoYi OpenAPI集成从单体到微服务改造全过程记录
java·运维·vue.js·spring cloud·微服务·云原生·架构