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

相关推荐
ch.ju12 小时前
Java Programming Chapter 2-Nested call
java·开发语言
iAm_Ike12 小时前
placeholder属性在IE9下有效吗_低版本表单提示替代方法【技巧】
jvm·数据库·python
牧瀬クリスだ12 小时前
MYSQL的索引与事务
数据库·mysql
杨充13 小时前
03.计算机基础CPU设计
java·开发语言
无所事事O_o13 小时前
【监控报警体系建设】监控标准与最佳实践
java·架构·监控
李木子qaq13 小时前
什么是数据库代理
数据库·proxy模式
LNN202213 小时前
半导体设备上位机 Qt 实现计划
开发语言·数据库·qt
牧瀬クリスだ13 小时前
MYSQL的视图
数据库·mysql
摇滚侠13 小时前
Java 项目教程《黑马商城》OpenFeign 15 - 19
java·开发语言
小小小米粒13 小时前
线程池创建模版
java