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

相关推荐
huipeng926几秒前
企业级微服务开发实战(三):公共模块设计与统一规范封装
java·spring boot·spring cloud·微服务·架构·系统架构·php
我登哥MVP6 分钟前
Spring Boot 从“会用”到“精通”:参数绑定体系全景
java·spring boot·spring·servlet·maven·intellij-idea·mybatis
C137的本贾尼8 分钟前
JDBC 编程:用 Java 连接 MySQL
java·开发语言·mysql
我是一颗柠檬15 分钟前
【Redis】Redis性能优化Day14(2026年)
数据库·redis·性能优化
华大哥15 分钟前
spring boot 和php 调用 LibreOffice 转换 Excel 到 PDF 完整实现
java·pdf·excel
微风欲寻竹影19 分钟前
Java数据结构——二叉树相关OJ题目详解
java·数据结构
微风欲寻竹影20 分钟前
Java数据结构——二叉树(Binary Tree)详解
java·数据结构·算法
程序员老油条21 分钟前
用 AI 生成复杂 SQL:LangChain4j + 本地模型实践
数据库·人工智能·sql
IT邦德24 分钟前
Oracle 26ai RAC 通过gold image RU打补丁
数据库·oracle
smith成长之旅24 分钟前
08 | Mem0 框架分析: BM25 的 Sigmoid 归一化
数据库·python·算法