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

相关推荐
LabVIEW开发4 分钟前
NI Package Manager安装LabVIEW时InternalServerError错误
网络·数据库·labview·labview知识·labview功能·labview程序
小钻风336610 分钟前
Spring Boot 文件上传详解:深入理解 MultipartFile 的使用与原理
java·开发语言
SelectDB12 分钟前
PostgreSQL 实时同步到 Apache Doris:一站式 CDC 解决方案
数据库
其美杰布-富贵-李13 分钟前
Spring Boot 工程开发全流程说明
java·spring boot·后端
Database_Cool_44 分钟前
AI Agent 长期记忆怎么存?阿里云 PolarDB-X 对接 Mem0 记忆框架实践
数据库·阿里云
会编程的土豆1 小时前
数据库范式
数据库·oracle
L1624761 小时前
MySQL 全环境生产快速安装 + 完整配置手册(整合国产银河麒麟适配 + 主从补充版)
数据库·mysql
yio_yin1 小时前
Spring事务管理(数据一致性)
数据库·sql·spring
X Chow1 小时前
MongoDB Beginner Guide 01:理解 NoSQL、文档模型与 Python 开发环境
数据库·mongodb·nosql
前端双越老师2 小时前
如何以前端视角(非0基础)学 Java ?
java·node.js·全栈