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

相关推荐
郑州光合科技余经理24 分钟前
同城商城源码:商品库和订单库怎么随包交付
android·java·开发语言·后端·uni-app·php·ai编程
\光辉岁月/25 分钟前
4.String类
java·面向对象·stringbuilder·string·stringbuffer·字符串类
极限挑战哥31 分钟前
深度拆解团购多商户分账系统:规则引擎 + 账户体系 + 逆向清算全解析
网络·数据库·人工智能
数据狐(Datafox)39 分钟前
一文读懂京东商品详情 API:返回字段解读与 JSON 样例
开发语言·数据库·爬虫·json
蚰蜒螟1 小时前
从 Java SocketChannel 到 TCP 挥手:一次 close() 的完整调用链与引用计数解析
java·python·tcp/ip
damoluomu1 小时前
国产 Java CMS 的协议和坑,讲清楚
java·开发语言
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 黑名单:分页查询黑名单
java·开发语言·交友
zyseo81 小时前
谷歌SEO 抓取日志分析脚本:解析访问日志,看穿 Googlebot 的抓取行为
大数据·数据库·搜索引擎·自动化
蚰蜒螟1 小时前
从 Java write 到磁盘扇区:一次写入操作的完整内核穿越之旅
java·开发语言