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

相关推荐
DBA小马哥13 分钟前
时序数据库是什么?能源行业国产化替换的入门必看
数据库·时序数据库
后端AI实验室16 分钟前
我把一个生产Bug的排查过程,交给AI处理——20分钟后我关掉了它
java·ai
凉年技术2 小时前
Java 实现企业微信扫码登录
java·企业微信
爱可生开源社区2 小时前
某马来西亚游戏公司如何从 SQL Server 迁移至 OceanBase?
数据库
狂奔小菜鸡3 小时前
Day41 | Java中的锁分类
java·后端·java ee
hooknum3 小时前
学习记录:基于JWT简单实现登录认证功能-demo
java
程序员Terry4 小时前
同事被深拷贝坑了3小时,我教他原型模式的正确打开方式
java·设计模式
NE_STOP4 小时前
MyBatis-缓存与注解式开发
java
码路飞4 小时前
不装 OpenClaw,我用 30 行 Python 搞了个 QQ AI 机器人
java
小瓦码J码4 小时前
PostgreSQL表名超长踩坑记
数据库·postgresql