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

相关推荐
ZhengEnCi10 分钟前
微信用SQLite为什么还要WCDB:聊天记录加密并发与坏库修复的工程真相
数据库·微信·sqlite
挖掘狂人1 小时前
如何保证缓存与数据库一致性
数据库·后端·程序员
SimonKing1 小时前
别再自己写同步脚本了!这款开源中间件,一套界面搞定 22 种数据源
java·后端·程序员
codigger1 小时前
如何保证缓存与数据库一致性
数据库·缓存·程序员
星落梦生1 小时前
苍穹外卖day3——阿里云oss文件上传
java·开发语言
头发还在的女程序员2 小时前
Spring Boot云快充协议接入:Netty报文分帧、长度检查与校验处理设计
java·spring boot·后端·充电桩·充电桩小程序·云快充
游哇游哇游2 小时前
Redis 过期删除与内存淘汰源码级详解:惰性+定期删除、近似 LRU/LFU、8 种策略与内存打满排查
数据库·redis·缓存·面试·bootstrap
星落梦生2 小时前
苍穹外卖day05——Redis
数据库·redis·缓存
Mr.Java.2 小时前
JetCache自定义序列化实战,Kryo/Protostuff编解码器一步接入
java·spring boot·spring
果果软件12 小时前
2026 后台管理系统选型:若依、JeecgBoot、芋道、NocoBase 和 GewuMind Boot 怎么选?
java·人工智能·后端·ai·开源