连接外部应用程序
Databases for EnterpriseDB 已弃用。 从 2025 年 6 月 16 日起,您将无法部署新的实例。 现有实例的支持服务将持续到2025年10月15日。 届时仍存在的任何实例将被删除。 更多信息,请参阅 Databases for EnterpriseDB 的弃用。
应用程序和驱动程序使用连接字符串来建立与 IBM Cloud® Databases for EnterpriseDB的连接。 该服务专门为驱动程序和应用程序提供连接字符串。 连接字符串显示在部署的“概述”的“端点”窗格中,还可以从 Cloud Databases CLI 插件 和 Cloud Databases API 中进行检索。
连接字符串可以由您在部署中创建的任何凭证使用。 虽然您可以将管理用户用于所有连接和应用程序,但最好专门为要连接的应用程序创建用户。 创建用户和获取连接字符串 页面上有关于生成凭证的文档。
使用语言驱动程序进行连接
驱动程序与部署建立连接所需的所有信息都在连接字符串的 "postgres" 部分中。 该表包含供参考的细目。
字段名称 | 索引 | 描述 |
---|---|---|
Type |
连接类型-对于 IBM Cloud® Databases for EnterpriseDB,它是 "URI" | |
Scheme |
URI 的方案-对于 IBM Cloud® Databases for EnterpriseDB,它是 "postgresql" | |
Path |
URI 的路径-对于 IBM Cloud® Databases for EnterpriseDB,它是数据库名称。 默认的邮箱地址是 ibmclouddb 。 |
|
Authentication |
Username |
用于连接的用户名。 |
Authentication |
Password |
用户的密码-可能显示为 $PASSWORD |
Authentication |
Method |
如何进行认证;“直接”认证由驱动程序处理。 |
Hosts |
0... |
要连接到的主机名和端口 |
Composed |
0... |
组合方案,认证,主机和路径的 URI |
Certificate |
Name |
用于数据库部署的自签名证书的已分配名称 |
Certificate |
Base64 | 证书的 base64 编码版本。 |
0...
指示数组中可能有一个或多个这些条目。
当给定在连接信息的“组合”字段中找到 URI 格式的连接字符串时,许多 PostgreSQL 驱动程序能够与您的部署建立连接。 例如
postgres://ibm_cloud_30399dec_4835_4967_a23d_30587a08d9a8:$PASSWORD@981ac415-5a35-4ac7-b6bb-fb609326dc42.8f7bfd8f3faa4218aec56e069eb46187.databases.appdomain.cloud:32704/ibmclouddb?sslmode=verify-full
以下示例使用连接字符串和 Java 驱动程序 jdbc 中的信息来连接到数据库。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.*;
public class PGConnect {
private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private Connection connect() {
final String url = "jdbc:postgresql://host:port/ibmclouddb";
Properties props = new Properties();
props.setProperty("user","admin");
props.setProperty("password","mypassword123");
props.setProperty("ssl","true");
props.setProperty("sslmode","verify-full");
props.setProperty("sslrootcert", "/path/to/cert");
Connection conn = null;
while (conn == null) {
try {
conn = DriverManager.getConnection(url, props);
System.out.println("Connected to PG");
} catch (SQLException e) {
System.out.printf("%s\n", e);
LOGGER.info("Not connected, retying ...");
}
}
return conn;
}
public static void main(String[] args) {
PGConnect icd = new PGConnect();
try {
Connection connection = icd.connect();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from pg_database");
while (rs.next()) {
System.out.println("DB Name: " + rs.getString(1));
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
以下示例使用连接字符串和 Python 驱动程序 Psycopg2
中的信息来连接到数据库。 这只是一个简单的连接示例,没有错误处理或重试逻辑,可能不适合生产。
import psycopg2
try:
conn = psycopg2.connect(
host="hostname.databases.appdomain.cloud",
port= 31525,
user="username",
password="password",
sslmode="verify-full",
sslrootcert="/path/to/cert/ca-certificate.crt",
database="ibmclouddb")
except:
print("Unable to connect to database")
cur = conn.cursor()
cur.execute("SELECT datname FROM pg_database")
rows = cur.fetchall()
print("List of databases:")
for row in rows:
print(" ",row[0])
此最终示例使用来自连接字符串和 Node driver node-postgres 的信息来连接到数据库。
const pg = require("pg");
const fs = require("fs");
let connectionString = "postgres://<username>:<password>@<host>:<port>/<database>?sslmode=verify-full";
let caCert = fs.readFileSync('/path/to/cert');
// set up a client with your Databases for EnterpriseDB connection string
let client = new pg.Client({ connectionString: connectionString,
// set up the TLS options
ssl: {
ca: caCert,
rejectUnauthorized: true
}
});
client.connect(function(err) {
if (err) {
console.log(err);
process.exit(1);
} else {
// query for the names of the databases
client.query(
"SELECT datname FROM pg_database;",
function(err, result) {
if (err) {
console.log(err);
}
// return the names of the databases
console.log(result.rows);
client.end();
}
);
}
});
驱动程序 TLS 和自签名证书支持
与 IBM Cloud® Databases for EnterpriseDB 的所有连接都已启用 TLS 1.2,因此用于连接的驱动程序需要能够支持加密。 您的部署还随附了自签名证书,因此驱动程序可以在连接时验证服务器。
有关更多信息,请参阅 Cloud Databases 证书常见问题。
使用自签名证书
- 从 端点 窗格或连接信息的 Base64 字段复制证书信息。
- 如果需要,将 Base64 字符串解码为文本。
- 将证书保存到文件中。 (可以使用提供的名称或您自己的文件名)。
- 向驱动程序或客户机提供证书的路径。
自签名证书的 CLI 插件支持
您可以使用命令 ibmcloud cdb deployment-cacert "your-service-name"
通过 CLI 插件显示用于部署的解码证书。 它将 base64 解码为文本。 复制命令的输出并将其保存到文件中,然后提供该文件的驱动程序路径。
其他驱动程序
PostgreSQL 具有大量语言驱动程序,这些驱动程序还可用于连接到 IBM Cloud® Databases for EnterpriseDB 部署。 此表涵盖一些最常见的驱动程序。
语言 | 驱动程序 | 示例 |
---|---|---|
PHP | pgsql |
链接 |
Ruby | ruby-pg |
链接 |
Ruby on Rails | Rails | 导轨指南 |
C# | ODBC |
链接 |
执行 | pq |
链接 |
Node | node-postgres |
链接 |
JDBC | EDB Postgres JDBC 连接器 | [JDBC 指南](https://www.enterprisedb.com/edb-docs/d/jdbc-connector/user-guides/jdbc-guide/42.2.8.1/ using_the_advanced_server_jdbc_connector_with_java_applications.html) |