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

相关推荐
pupudawang2 分钟前
如何查询SQL Server数据库服务器的IP地址
java
SimonKing2 分钟前
IntelliJ IDEA 配置与插件全部迁移到其他盘,彻底释放C盘空间
java·后端·程序员
华科易迅5 分钟前
Spring 代理
java·后端·spring
我真会写代码7 分钟前
深度解析ConcurrentHashMap:从底层原理到生产实战,搞定并发安全映射(含面试避坑)
java·并发编程
F1FJJ8 分钟前
Shield CLI v0.3.0:插件系统上线,首发 MySQL Web 管理
网络·数据库·网络协议·mysql·容器·golang
liangdabiao10 分钟前
XHS_Business_Idea_Validator-小红书解析市场机会智能体
java·ide·intellij-idea
波波七11 分钟前
maven导入spring框架
数据库·spring·maven
深蓝轨迹12 分钟前
Redis 分布式锁实现流程
数据库·redis·分布式
程序猿阿伟14 分钟前
《OpenClaw端口通信失效全解:监听修改与防火墙规则落地指南》
服务器·数据库·windows
进击的雷神14 分钟前
突破增量抓取困境:基于数据库状态判断的高效新闻爬虫设计
数据库·爬虫·spiderflow