在[OCI]自主数据库上创建数据库链接并尝试检索Aurora PostgreSQL中的数据

首先

现在,Autonomous Database可以创建数据库链接来连接除了Oracle以外的数据库,如Amazon Redshift,MySQL,PostgreSQL和Snowflake。

 

目前,以下前提条件需要满足:
– 目标数据库可以从公共互联网访问
– 目标数据库已配置为允许SSL/TLS连接。

所以,這次我們在Autonomous Database中建立了一個數據庫連結到Aurora PostgreSQL,並嘗試從Autonomous Database中參考Aurora PostgreSQL中的數據。

1. 准备 Aurora PostgreSQL

本次操作是在AWS的东京区域(ap-northeast-1)创建了Aurora PostgreSQL,并允许来自公共互联网的连接。
Aurora PostgreSQL默认允许SSL/TLS连接。

使用psql命令连接到Aurora PostgreSQL。

[opc@pg ~]$ psql -U postgres -h xxxxxx-instance-1.aaaaaaaaaaaa.ap-northeast-1.rds.amazonaws.com
Password for user postgres: 
psql (14.2, server 13.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=>

创建名为 testdb 的数据库。

postgres=> CREATE DATABASE testdb;
CREATE DATABASE
postgres=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 rdsadmin  | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | rdsadmin=CTc/rdsadmin
 template0 | rdsadmin | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/rdsadmin          +
           |          |          |             |             | rdsadmin=CTc/rdsadmin
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

postgres=>

我将连接切换到 testdb。

postgres=> \c testdb
psql (14.2, server 13.6)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
You are now connected to database "testdb" as user "postgres".
testdb=>

创建一个名为testtable的表,并添加数据。

testdb=> CREATE TABLE testtable (
testdb(>   id INTEGER, 
testdb(>   name VARCHAR(100)
testdb(> );
CREATE TABLE
testdb=> INSERT INTO testtable VALUES (1,'Name1');
INSERT 0 1
testdb=> INSERT INTO testtable VALUES (2,'Name2');
INSERT 0 1
testdb=> INSERT INTO testtable VALUES (3,'Name3');
INSERT 0 1
testdb=> select * from public.testtable;
 id | name  
----+-------
  1 | Name1
  2 | Name2
  3 | Name3
(3 rows)

testdb=>

我要退出psql。

testdb=> exit
[opc@pg ~]$

2. 连接到自主数据库的方式

使用SQL*Plus连接到位于东京区域的Autonomous Database。

[opc@pg ~]$ sqlplus admin/Demo#1Demo#1@testatp1_medium

SQL*Plus: Release 21.0.0.0.0 - Production on Wed Apr 6 07:04:30 2022
Version 21.5.0.0.0

Copyright (c) 1982, 2021, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.15.0.1.0

SQL>

创建用于连接Autonomous Database到Aurora PostgreSQL的凭据。

使用DBMS_CLOUD.CREATE_CREDENTIAL存储过程在自主数据库中创建Aurora PostgreSQL连接所需的凭据AURORA_PG_CRED。
在username参数中指定PostgreSQL用户的用户名,在password参数中指定该用户的密码。

SQL> BEGIN
  2    DBMS_CLOUD.CREATE_CREDENTIAL(
  3       credential_name => 'AURORA_PG_CRED',
  4       username => 'postgres',
  5       password => 'Demo#1Demo#1');
  6  END;
  7  /

PL/SQL procedure successfully completed.

SQL>

创建一个数据库链接,用于从自主数据库连接到Aurora PostgreSQL。

使用DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK存储过程创建从Autonomous Database到Aurora PostgreSQL的数据库链接。
指定每个DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK参数如下。

項目値db_link_name作成するデータベース・リンクの名前hostnameRDSマネジメントコンソールに表示されるAurora PostgreSQLのエンドポイントportRDSマネジメントコンソールに表示されるAurora PostgreSQLのポートservice_nameデータベース名credential_name3.で作成したAurora PostgreSQLに接続するためのクレデンシャルの名前gateway_params接続先DBに応じてdb_typeを設定、PostgreSQLの場合は’POSTGRES’。詳細はこちらを参照directory_nameNULLssl_server_cert_dnNULL
SQL> BEGIN
  2    DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK(
  3            db_link_name => 'AURORA_PG_LINK', 
  4            hostname => 'xxxxxx-instance-1.aaaaaaaaaaaa.ap-northeast-1.rds.amazonaws.com', 
  5            port => '5432',
  6            service_name => 'testdb',
  7            credential_name => 'AURORA_PG_CRED',
  8            gateway_params => JSON_OBJECT('db_type' value 'POSTGRES'),
  9            directory_name => NULL,
 10            ssl_server_cert_dn => NULL);
 11  END;
 12  /

PL/SQL procedure successfully completed.

SQL>

通过数据库链接从自治数据库中查看Aurora PostgreSQL内的数据。

我已成功创建了从Autonomous Database到Aurora PostgreSQL的数据库链接,请尝试通过Autonomous Database的数据库链接来访问Aurora PostgreSQL中的数据。

在指定参考表时,需用小写的表名用双引号括起来,例如”testtable”。
在指定模式并引用表时,也需要以同样的方式书写,即”模式名”.”表名”。

SQL> col name for a20
SQL> SELECT * FROM "testtable"@AURORA_PG_LINK;

	    id name
---------- --------------------
	     1 Name1
	     2 Name2
	     3 Name3

SQL> 

我们可以通过数据库链接从Autonomous Database访问Aurora PostgreSQL中的数据。

请提供更多上下文来帮助我准确理解您的需求。

使用Oracle托管的异构连接创建与非Oracle数据库的数据库连接。
CREATE_CREDENTIAL过程
CREATE_DATABASE_LINK过程
从您的自主数据库访问Amazon Redshift、Snowflake和其他非Oracle数据库。

bannerAds