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

相关推荐
ATA8888几秒前
数据库管理工具的数据安全机制对比:权限管控与审计
数据库·人工智能·数据分析·数据库管理
momo4 分钟前
JAVA基础知识
java·开发语言
麦聪聊数据22 分钟前
业务自助取数(下):安全可控前提下,实现取数效率百倍提升
数据库·sql
2501_9157167231 分钟前
Repository-学习指南_小白版
数据库·人工智能
snpgroupcn35 分钟前
SAP实施风险管控注意事项:四大风险域与防范策略
前端·数据库·人工智能
就叫_这个吧41 分钟前
Activiti7工作流入门+案例应用
java·activiti7·工作流
Z_W_H_1 小时前
minikube start --driver=docker --memory=4000mb出现PROVIDER_DOCKER_NEWGRP
java·docker·minikube
折哥的程序人生 · 物流技术专研1 小时前
第3篇:手写促销策略引擎(满减、打折、立减)
java·设计模式·策略模式·开闭原则·编程实战·扩充系列·促销引擎
cyforkk1 小时前
MySQL锁机制详解:从乐观锁到悲观锁
数据库·mysql
ShiXZ2131 小时前
指令集-SQL 常用指令速查手册
数据库·sql·mysql·oracle