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

相关推荐
Book思议-7 分钟前
顺序表和链表核心差异与优缺点详解
java·数据结构·链表
cyber_两只龙宝15 分钟前
【MySQL】MySQL主从复制架构
linux·运维·数据库·mysql·云原生·架构
D.不吃西红柿15 分钟前
【无标题】
数据库·database·ai编程·数据库设计
Dylan~~~18 分钟前
PostgreSQL 数据库性能问题定位完全指南
数据库·postgresql
原来是猿22 分钟前
MySQL数据库基础
数据库
江不清丶28 分钟前
Text-to-SQL实战:从自然语言到数据库查询的智能数据分析Agent设计
数据库·sql·ai·数据分析
小杨的博客31 分钟前
Java + Selenium实现浏览器打印功能
java·selenium
wefly201732 分钟前
M3U8 播放调试天花板!m3u8live.cn纯网页无广告,音视频开发效率直接拉满
java·前端·javascript·python·音视频
兆子龙36 分钟前
antd 组件也做了同款效果!深入源码看设计模式在前端组件库的应用
java·前端·架构
初次攀爬者36 分钟前
Redis与数据库的数据一致性方案解析
数据库·redis·分布式