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

相关推荐
Highcharts.js5 小时前
成分占比饼图代码|常用可视化图表开发示例
java·开发语言·仪表盘·highcharts·实战代码·响应式图表
2401_891409265 小时前
美股多周期分钟K线、逐笔成交、历史订单簿、财务和基础信息数据包
数据库
工业一体机老司机6 小时前
rsync+inotify 收集车间工位机日志的完整配置
服务器·数据库·postgresql
西柚小萌新7 小时前
【人工智能:LangGraph】--3.控制流(Edge)
java·服务器·数据库
金銀銅鐵7 小时前
带您看懂字节码指令(八):数组里有什么
java·jvm·后端
鲁邦通物联网7 小时前
DP动力定位系统网关替换实践:通信报文、接口迁移与测试基线设计
网络·数据库·海事网关·海事网络安全·海事网络安全网关·智慧航运·船舶远程监控
oradh7 小时前
Oracle数据块内部研究(四)
数据库·oracle
EatFan8 小时前
MySQL 与 Redis 双写一致性实战:从 Cache-Aside 到延迟双删,再到 Binlog 兜底
数据库·redis·mysql
r_oo_ki_e_9 小时前
牛牛与切割机
java·算法
IvorySQL9 小时前
PostgreSQL 日报|布尔列 NOT 选择率估算偏高(12 月 4 日)
数据库·postgresql