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

相关推荐
草莓熊Lotso几秒前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构
星恒随风13 分钟前
MySQL 从零入门(十一):视图与用户权限管理详解
linux·服务器·数据库·笔记·mysql
天翼云开发者社区14 分钟前
天翼云TeleDB最佳实践:某大型企业平滑去O,国产化替代一步到位
数据库
青山不见16 分钟前
告别手动拼参数!IDEA 插件 Log2SQL:MyBatis/Hibernate 日志秒变可执行 SQL
数据库
Rain的Java大神之路23 分钟前
把数组本身变成哈希表:原地哈希搞定 LeetCode 41「缺失的第一个正数」
java·后端·leetcode
山楂树の1 小时前
Java 泛型:泛型类、泛型方法、泛型接口与通配符
java·开发语言·笔记
李兆龙的博客1 小时前
从一到无穷大 #115:企业级 SSD 涨价后的容量利用率与磨损管理
数据库·ssd·nvme
跳跳的小古风2 小时前
前端转全栈:一条接口从前端请求到 SQL 完整流程
java·前端
jianqiang.xue3 小时前
把Agent塞进MCU:工具次数和上下文长度怎么卡
java·前端·网络·单片机·esp32·ai编程
ikgy4 小时前
JAVA EE:4.多线程核心组件实战
java·java-ee