PostgreSQL接MySQL外表

2023年 8月 18日 81.1k 0

Postgresql下安装mysql_fdw

设置环境变量(根据实际安装路径变更)

export PATH=/usr/pgsql-14/bin/:$PATH
export PATH=/usr/local/mysql/bin/:$PATH
make USE_PGXS=1 #编译
make USE_PGXS=1 install " #安装

创建EXTENSION

CREATE EXTENSION mysql_fdw;

创建SERVER

CREATE SERVER mysql_server
FOREIGN DATA WRAPPER mysql_fdw
OPTIONS (host '127.0.0.1', port '3306');

创建USER MAPPING

CREATE USER MAPPING FOR postgres
SERVER mysql_server
OPTIONS (username 'root', password '123456');"

创建FOREIGN TABLE

CREATE FOREIGN TABLE warehouse(
warehouse_id int,
warehouse_name text,
warehouse_created timestamp)
SERVER mysql_server
OPTIONS (dbname 'mysql', table_name 'warehouse');"

创建TABLE

CREATE TABLE warehouse(
warehouse_id int primary key not null,
warehouse_name text,
warehouse_created timestamp
);"

插入数据

INSERT INTO warehouse values (1, 'UPS', now());
INSERT INTO warehouse values (2, 'TV', now());
INSERT INTO warehouse values (3, 'Table', now());

修改数据

update warehouse set warehouse_name='new name' where warehouse_id=2;
select * from warehouse;

删除数据

delete from warehouse where warehouse_id=3;
select * from warehouse;

查询 PG数据

select * from warehouse;

插入数据

INSERT INTO warehouse values (3, 'Table', now());
INSERT INTO warehouse values (4, 'NEWS', '2020-06-02 10:00:00');
select * from warehouse;

查询mysql数据

select * from warehouse;

修改数据

update warehouse set warehouse_name='New Name' where warehouse_id=3;
select * from warehouse;

查询mysql数据

select * from warehouse;

删除数据

delete from warehouse where warehouse_id=4;

查询mysql数据

select * from warehouse;

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论