PostgrSQL binding spec

Detailed documentation on the PostgreSQL binding component

配置

要设置与 PostgreSQL相关的 绑定,需要创建类型 bindings.postgres 的组件。 请参阅本指南,了解如何创建和应用绑定配置。

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: bindings.postgres
  8. version: v1
  9. metadata:
  10. - name: url # Required
  11. value: <CONNECTION_STRING>

Warning

以上示例将 Secret 明文存储。 更推荐的方式是使用 Secret 组件, 这里

元数据字段规范

字段必填绑定支持详情示例
urlYOutputPostgres连接字符串的写法,请参阅此处“user=dapr password=secret host=dapr.example.com port=5432 dbname=dapr sslmode=verify-ca”

URL格式

The PostgreSQL binding uses pgx connection pool internally so the url parameter can be any valid connection string, either in a DSN or URL format:

Example DSN

  1. user=dapr password=secret host=dapr.example.com port=5432 dbname=dapr sslmode=verify-ca

Example URL

  1. postgres://dapr:secret@dapr.example.com:5432/dapr?sslmode=verify-ca

Both methods also support connection pool configuration variables:

  • pool_min_conns: integer 0 or greater
  • pool_max_conns: integer greater than 0
  • pool_max_conn_lifetime: duration string
  • pool_max_conn_idle_time: duration string
  • pool_health_check_period: duration string

绑定支持

该组件支持输出绑定,其操作如下:

  • exec
  • query
  • close

exec

The exec operation can be used for DDL operations (like table creation), as well as INSERT, UPDATE, DELETE operations which return only metadata (e.g. number of affected rows).

请求

  1. {
  2. "operation": "exec",
  3. "metadata": {
  4. "sql": "INSERT INTO foo (id, c1, ts) VALUES (1, 'demo', '2020-09-24T11:45:05Z07:00')"
  5. }
  6. }

响应

  1. {
  2. "metadata": {
  3. "operation": "exec",
  4. "duration": "294µs",
  5. "start-time": "2020-09-24T11:13:46.405097Z",
  6. "end-time": "2020-09-24T11:13:46.414519Z",
  7. "rows-affected": "1",
  8. "sql": "INSERT INTO foo (id, c1, ts) VALUES (1, 'demo', '2020-09-24T11:45:05Z07:00')"
  9. }
  10. }

query

The query operation is used for SELECT statements, which returns the metadata along with data in a form of an array of row values.

请求

  1. {
  2. "operation": "query",
  3. "metadata": {
  4. "sql": "SELECT * FROM foo WHERE id < 3"
  5. }
  6. }

响应

  1. {
  2. "metadata": {
  3. "operation": "query",
  4. "duration": "432µs",
  5. "start-time": "2020-09-24T11:13:46.405097Z",
  6. "end-time": "2020-09-24T11:13:46.420566Z",
  7. "sql": "SELECT * FROM foo WHERE id < 3"
  8. },
  9. "data": "[
  10. [0,\"test-0\",\"2020-09-24T04:13:46Z\"],
  11. [1,\"test-1\",\"2020-09-24T04:13:46Z\"],
  12. [2,\"test-2\",\"2020-09-24T04:13:46Z\"]
  13. ]"
  14. }

close

Finally, the close operation can be used to explicitly close the DB connection and return it to the pool. This operation doesn’t have any response.

请求

  1. {
  2. "operation": "close"
  3. }

Note, the PostgreSql binding itself doesn’t prevent SQL injection, like with any database application, validate the input before executing query.

相关链接