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

相关推荐
zhoutongsheng几秒前
CSS如何使用-hover显示图片文字说明_利用--after实现图文叠加效果
jvm·数据库·python
2301_783848654 分钟前
CSS解决浮动元素导致的布局闪烁_稳定容器布局高度
jvm·数据库·python
m0_740796365 分钟前
Workerman5.0协程实战:PHP高并发新标准
jvm·数据库·python
2301_769340675 分钟前
如何在 CSS 中实现元素的绝对定位,使其不受窗口尺寸变化影响
jvm·数据库·python
m0_7020365312 分钟前
防止SQL注入的运维实践_实时清理数据库缓存与历史记录
jvm·数据库·python
ch.ju12 分钟前
Java程序设计(第3版)第二章——函数的递归
java·开发语言
2301_7796224114 分钟前
Redis怎样合并多天访客数据_通过PFMERGE指令聚合HyperLogLog记录
jvm·数据库·python
m0_7485548114 分钟前
如何监控集群 interconnect_ping与traceroute验证心跳通畅
jvm·数据库·python
2301_7756398916 分钟前
Redis怎样查询集群的整体健康状态_使用cluster info指令查看槽位覆盖率与节点状态
jvm·数据库·python
zhoutongsheng17 分钟前
CSS如何处理带有状态切换的折叠菜单_利用BEM修饰符管理状态
jvm·数据库·python