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

相关推荐
zcn1261 分钟前
关于非相关子查询改写经验
数据库·sql·sql优化改写
追梦开发者5 分钟前
MongoDB 踩坑实录②:数据建模和索引没搞对,查询慢了整整 10 倍
数据库·mongodb·database
我只想困告7 分钟前
day02-RabbitMQ 2026-05-14
java·spring·rabbitmq
KaMeidebaby12 分钟前
卡梅德生物技术快报|单克隆抗体人源化 PEG 修饰质控方法体系构建与验证
服务器·前端·数据库·人工智能·算法·百度·新浪微博
Nontee19 分钟前
Java 后端面试题目全集
java·开发语言·面试
2401_8246976619 分钟前
mysql添加索引导致插入变慢怎么办_索引优化与异步处理方案
jvm·数据库·python
2401_8246976623 分钟前
Go语言如何写负载均衡器_Go语言负载均衡器实战教程【完整】
jvm·数据库·python
m0_7335654623 分钟前
CSS如何快速微调项目的间距大小_使用CSS变量批量修改值
jvm·数据库·python
Languorous.29 分钟前
MySQL聚合查询:COUNT、SUM、AVG用法,实战案例演示
android·数据库
woxihuan12345631 分钟前
如何为禁用按钮添加点击提示信息
jvm·数据库·python