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

相关推荐
Han.miracle2 分钟前
Java线程的学习—多线程(一)
java·开发语言·学习
人间打气筒(Ada)4 分钟前
yum安装k8s集群----基于centos7.9
java·容器·kubernetes
冠希陈、12 分钟前
PHP7.4.33 安装sqlsrv扩展
数据库
光影341517 分钟前
专利撰写与申请核心要点简报
前端·数据库·php
Filotimo_31 分钟前
Spring MVC 数据校验
java·spring·mvc
心灵宝贝38 分钟前
申威ky10架构安装MongoDB 4.0.1(rpm包:mongodb-4.0.1-8.ky10.sw_64.rpm)详细步骤
数据库·mongodb·架构
沐浴露z38 分钟前
Kafka Consumer 详解API,分区分配策略以及消费offset
java·kafka
weixin_404551241 小时前
openrewrite Lossless Semantic Trees (LST)
java·tree·yaml·lossless·openrewrite·lst·semantic
一 乐1 小时前
口腔健康系统|口腔医疗|基于java和小程序的口腔健康系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·小程序·毕设
YDS8291 小时前
苍穹外卖 —— 文件上传和菜品的CRUD
java·spring boot·后端