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

相关推荐
Wang15303 小时前
Java排序
java
逸风尊者3 小时前
XGBoost模型工程使用
java·后端·算法
一嘴一个橘子3 小时前
MP 自定义业务方法 (二)
java
低客的黑调3 小时前
MyBatis-Plus-从 CRUD 到高级特性
java·servlet·tomcat
南境十里·墨染春水3 小时前
linux学习进展 进程间通讯——共享内存
linux·数据库·学习
就像风一样抓不住3 小时前
Java 手机号校验工具类
java
斯维赤4 小时前
Python学习超简单第八弹:连接Mysql数据库
数据库·python·学习
凤山老林4 小时前
26-Java this 关键字
java·开发语言
Chuer_4 小时前
讲透财务Agent核心概念,深度拆解财务Agent应用趋势
大数据·数据库·安全·数据分析·甘特图
gushinghsjj4 小时前
什么是主数据管理平台?怎么构建主数据管理平台?
大数据·数据库