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

相关推荐
窝子面24 分钟前
手搓最简前后端协作-node
javascript·数据库
吳所畏惧36 分钟前
宝塔面板Redis密码修改指南:SSH命令修改 vs 面板UI界面修改,哪个更靠谱?
运维·服务器·数据库·redis·缓存·ssh
DFT计算杂谈39 分钟前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
HPFBoy1 小时前
log4net 数据库存日志正确配置
数据库
liuxiaowei31 小时前
【绝版教程】新版MySQL DBA高级实战进阶班 MySQL8.0 姜承尧-腾讯数据库总监
数据库·mysql·dba
维天说2 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json
破碎的南瓜2 小时前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
Zane19942 小时前
并发 vs 并行:别再傻傻分不清了,一文讲透 Java 并发编程的第一课
java·后端
噢,我明白了3 小时前
java中Excel的导入和导出(EasyExcel)
java·开发语言·excel
吠品3 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库