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

相关推荐
谅望者4 分钟前
数据分析笔记02:数值方法
大数据·数据库·笔记·数据挖掘·数据分析
CodeAmaz8 分钟前
统一发包管理(Maven 仓库)详细步骤
java·maven·运维开发·个人开发
dreams_dream9 分钟前
django模型数据查询
数据库·django·sqlite
没有bug.的程序员19 分钟前
Spring Cloud Bus 事件广播机制
java·开发语言·spring boot·hystrix·feign·springcloudbus·事件广播机制
找不到、了24 分钟前
Java系统设计知识整理《1》
java·开发语言
郏国上27 分钟前
由于图片视频替换和删除导致阿里云上存在大量系统不再使用的文件如何处理
数据库·mongodb·阿里云
程序猿七度28 分钟前
【Excel导入】读取WPS格式嵌入单元格内的图片
java·开发语言·wps
用户2986985301442 分钟前
Java: 为PDF批量添加图片水印实用指南
java·后端·api
百***27111 小时前
UNION 和 UNION ALL 的区别:深入解析 SQL 中的合并操作
数据库·sql·oracle
dreams_dream1 小时前
Flask 和 Django 的详细对比
django·sqlite·flask