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

相关推荐
ybwycx25 分钟前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端
PrDf22Iw826 分钟前
CPU ↔ DRAM(内存总线)的可持续数据传输带宽
java·运维·网络
TDengine (老段)30 分钟前
TDengine IDMP 可视化 —— 面板
大数据·数据库·人工智能·物联网·ai·时序数据库·tdengine
代码改善世界33 分钟前
【matlab初阶】matlab入门知识
android·java·matlab
卓怡学长40 分钟前
m315基于java的水果网上商城的开发与设计
java·数据库·spring·tomcat·maven·intellij-idea
刘~浪地球1 小时前
Redis 从入门到精通(八):有序集合操作详解
数据库·chrome·redis
执笔画流年呀1 小时前
7大排序算法
java·算法·排序算法
fe7tQnVan1 小时前
MyBatis-动态sql与高级映射
数据库·sql·mybatis
计算机安禾1 小时前
【数据结构与算法】第28篇:平衡二叉树(AVL树)
开发语言·数据结构·数据库·线性代数·算法·矩阵·visual studio
zdl6862 小时前
springboot+全局异常处理
java·spring boot·spring