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

相关推荐
代码中介商7 小时前
Redis入门:5大数据类型全解析
数据库·redis·缓存
C+++Python7 小时前
C++ 进阶学习完整指南
java·c++·学习
渣渣盟8 小时前
数据库设计范式详解(纯小白版)
数据库·oracle·软考·数据库工程师
zhangjw348 小时前
第11篇:Java Map集合详解,HashMap底层原理、哈希冲突、JDK1.8优化、遍历方式彻底吃透
java·开发语言·哈希算法
还得是你大哥8 小时前
Java互联网医院管理系统源码SpringBoot
java·spring boot·vue
阿维的博客日记8 小时前
怎么用ThreadLocal解决用户的登录上下文
java·juc
Dicky-_-zhang9 小时前
容器编排实战:Docker与Kubernetes对比选型与落地实践
java·jvm
夜雪闻竹9 小时前
Cursor 对话导入:解析 SQLite 里的宝藏
数据库·sqlite·ai编程
benpaodeDD9 小时前
视频10,11,12,13——java程序的加载与执行,安装jdk
java·开发语言
数字化顾问9 小时前
(122页PPT)数字化架构的演进和治理(附下载方式)
java·运维·架构