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

相关推荐
CV_J2 小时前
安装kibana
java·elasticsearch·spring cloud·docker·容器
码农水水4 小时前
国家电网Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·网络·分布式·面试·wpf
木风小助理5 小时前
PostgreSQL基础知识——DDL深度解析
数据库·postgresql
hanqunfeng5 小时前
(四十四)Redis8 新增的数据类型 -- Vector Set
数据库·redis·缓存
qq_336313935 小时前
java基础-网络编程-TCP
java·网络·tcp/ip
咕噜咕噜啦啦5 小时前
Java期末习题速通
java·开发语言
盐真卿5 小时前
python2
java·前端·javascript
梦梦代码精6 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
一嘴一个橘子6 小时前
mybatis - 动态语句、批量注册mapper、分页插件
java
组合缺一6 小时前
Json Dom 怎么玩转?
java·json·dom·snack4