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

相关推荐
草履虫建模2 分钟前
A02 Maven 基础配置:本地仓库、镜像、项目编码与常见问题(IDEA 实战)
xml·java·spring boot·spring·maven·intellij-idea·idea
编程之升级打怪2 分钟前
Intellij IDEA常用快捷方式
java·intellij-idea
java1234_小锋5 分钟前
Java项目中如何选择垃圾回收器?
java·开发语言
酒书7 分钟前
Alibaba Cloud Toolkit插件实现idea一键打包部署(mac)
java·ide·intellij-idea
TracyCoder1237 分钟前
MySQL 实战宝典(二):MySQL vs Elasticsearch 文本检索性能全方位对比
数据库·mysql·elasticsearch
zhangjin11208 分钟前
java线程的阻塞和等待的区别
java·开发语言
shejizuopin9 分钟前
基于Spring Boot的高校科研管理系统的设计与实现(毕业论文)
java·spring boot·vue·毕业设计·论文·毕业论文·高校科研管理系统的设计与实现
m0_7375393710 分钟前
Mariadb 服务器
服务器·数据库·mariadb
indexsunny10 分钟前
互联网大厂Java面试实战:Spring Boot微服务在电商场景中的应用
java·数据库·spring boot·redis·微服务·kafka·电商
广药门徒13 分钟前
WS2812_CONTROL使用手册
android·java·数据库