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

相关推荐
庞轩px10 分钟前
深入理解 sleep() 与 wait():从基础到监视器队列
java·开发语言·线程··wait·sleep·监视器
卤炖阑尾炎17 分钟前
基于 MySQL 主主复制 + HAProxy+Keepalived 构建高可用集群实战
数据库·mysql
Dxy123931021624 分钟前
MySQL 如何高效删除大量数据:策略与最佳实践
数据库·mysql·oracle
皮皮林5511 小时前
面试官:ZSet 的底层实现是什么?
java
倔强的石头_1 小时前
从 “不得不存” 到 “战略必争”:工业数据的价值觉醒之路
数据库
码云数智-大飞1 小时前
C++ RAII机制:资源管理的“自动化”哲学
java·服务器·php
倔强的石头_1 小时前
新型电力系统应该用什么数据库?——时序数据库选型与落地实战
数据库
2601_949816581 小时前
Spring+Quartz实现定时任务的配置方法
java
南汐以墨2 小时前
一个另类的数据库-Redis
数据库·redis·缓存
计算机毕设指导62 小时前
基于SpringBoot校园学生健康监测管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea