You need to enable JavaScript to run this app.
导航
使用 Go SDK
最近更新时间:2024.04.28 13:20:21首次发布时间:2024.04.28 11:36:22

本文介绍云数据库 PostgreSQL 版 Go SDK 的下载、安装与初始化客户端的相关信息。

下载与安装

前提条件

需要使用 Go 1.5 或以上版本,推荐使用 Go 1.12 或以上版本。

说明

您可以执行 go version 命令检查当前 Go 版本信息。

SDK 下载地址

云数据库 PostgreSQL 版 Go SDK 源码地址,请参见 PostgreSQL Go SDK

安装 SDK

在目标 Go 项目下,执行以下命令安装 Go SDK。

go get github.com/volcengine/volcengine-go-sdk/volcengine/credentials
go get github.com/volcengine/volcengine-go-sdk/volcengine/volcengineutil

初始化客户端

云数据库 PostgreSQL 版 Go SDK 支持通过 Session 的方式初始化客户端。通过 Session 方式创建的 PostgreSQL 客户端,底层会实现 HTTP/HTTPS 资源(连接或请求)的复用。当需要初始化多个客户端进行操作时,通过 Session 方式创建可以提升客户端的性能。

前提条件

  • 已注册火山引擎账号并完成实名认证。具体步骤,请参见账号注册实名认证

  • 已获取账号的 Access Key ID(简称 AK)和 Secret Access Key(简称 SK)信息,获取方法请参见查看 AKSK 信息

  • 已获取云数据库 PostgreSQL 版支持的 Region,请参见服务地址

  • 已下载并安装云数据库 PostgreSQL 版 Go SDK。具体操作步骤,请参见下载与安装

初始化客户端

使用以下代码初始化客户端。

package rdspostgresql
import (
    "[github.com/volcengine/volcengine-go-sdk/volcengine](http://github.com/volcengine/volcengine-go-sdk/volcengine)"
    "[github.com/volcengine/volcengine-go-sdk/volcengine/credentials](http://github.com/volcengine/volcengine-go-sdk/volcengine/credentials)"
    "[github.com/volcengine/volcengine-go-sdk/volcengine/session](http://github.com/volcengine/volcengine-go-sdk/volcengine/session)"
    "[github.com/volcengine/volcengine-go-sdk/service/rdspostgresql](http://github.com/volcengine/volcengine-go-sdk/service/rdspostgresql)"
)
var (
    Config *volcengine.Config
    Sess   *session.Session
    Client *rdspostgresql.RDSPOSTGRESQL
)
const (
    AK       = "Your AK"
    SK       = "Your SK"
    REGION   = "Your Region"
    ENDPOINT = "Your Endpoint"
)
func InitConfig() {
    Config = volcengine.NewConfig().
       WithCredentials(credentials.NewStaticCredentials(AK, SK, "")).
       WithRegion(REGION).WithEndpoint(ENDPOINT)
    Sess, _ = session.NewSession(Config)
    Client = rdspostgresql.New(Sess)
}

代码示例

请求超时时间设置

使用 Go SDK 调用 PostgreSQL 接口时,默认无超时时间设置。 您可以在初始化客户端时,通过 WithHTTPClient(&http.Client{Timeout: <超时时间> * time.Second}) 来指定客户端的请求超时时间,若未在该时间内完成,请求会立即终止并报超时错误。例如,您可以通过以下代码将客户端的请求超时时间设置为 10 秒。

config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})

创建 PostgreSQL 实例

以下 SDK 代码中所使用的参数的定义、取值范围等信息,请参见 CreateDBInstance

package rdspostgresql
import (
    "fmt"
    "[github.com/volcengine/volcengine-go-sdk/service/rdspostgresql](http://rgithub.com/volcengine/volcengine-go-sdk/service/rdspostgresql)"
    "testing"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
)
func TestRDSPostgreSQL_CreateDBInstanceCommon(t *testing.T) {
    InitConfig()
    resp, err := Client.CreateDBInstance(&rdspostgresql.CreateDBInstanceInput{
       DBEngineVersion: volcengine.String("PostgreSQL_12"),
       NodeInfo: []*rdspostgresql.NodeInfoForCreateDBInstanceInput{
          {
             NodeOperateType: volcengine.String("Create"),
             NodeSpec:        volcengine.String("rds.postgres.1c2g"),
             NodeType:        volcengine.String("Primary"),
             ZoneId:          volcengine.String("cn-beijing-c"),
          },
          {
             NodeOperateType: volcengine.String("Create"),
             NodeSpec:        volcengine.String("rds.postgres.1c2g"),
             NodeType:        volcengine.String("Secondary"),
             ZoneId:          volcengine.String("cn-beijing-c"),
          },
          {
             NodeOperateType: volcengine.String("Create"),
             NodeSpec:        volcengine.String("rds.postgres.1c2g"),
             NodeType:        volcengine.String("ReadOnly"),
             ZoneId:          volcengine.String("cn-beijing-c"),
          },
       },
       StorageSpace: volcengine.Int32(30),
       StorageType:  volcengine.String("LocalSSD"),
       VpcId:        volcengine.String("vpc-2bzo8w6m87v282dx0eemg****"),
       SubnetId:     volcengine.String("subnet-2d6b6byrhtts058ozfenq****"),
       ChargeInfo: &rdspostgresql.ChargeInfoForCreateDBInstanceInput{
          ChargeType: volcengine.String("PrePaid"),
          AutoRenew:  volcengine.Bool(false),
          PeriodUnit: volcengine.String("Month"),
          Period:     volcengine.Int32(2),
          Number:     volcengine.Int32(1),
       },
       InstanceName: volcengine.String("pg-test-sdk-go"),
       ProjectName:  volcengine.String("default"),
       Tags: []*rdspostgresql.TagForCreateDBInstanceInput{
          {
             Key:   volcengine.String("test1"),
             Value: volcengine.String("test1"),
          },
          {
             Key:   volcengine.String("test2"),
             Value: volcengine.String("test2"),
          },
       },
    })
    if err != nil {
       panic(err)
    }
    fmt.Println(resp)
}

本示例中的参数说明如下表所示。

配置项说明取值
DBEngineVersion兼容版本。PostgreSQL_12

NodeInfo

实例规格配置。

[
{
"NodeType": "Primary",
"ZoneId": "cn-beijing-c",
"NodeSpec": "rds.postgres.1c2g"
},
{
"NodeType": "Secondary",
"ZoneId": "cn-beijing-c",
"NodeSpec": "rds.postgres.1c2g"
},
{
"NodeType": "ReadOnly",
"ZoneId": "cn-beijing-c",
"NodeSpec": "rds.postgres.1c2g"
}
]

StorageType实例存储类型。LocalSSD
StorageSpace实例存储空间。30
VpcId专有网络(VPC) ID。vpc-2bzo8w6m87v282dx0eemg****
SubnetId子网 ID。subnet-2d6b6byrhtts058ozfenq****
InstanceName实例名称。pg-test-sdk-go
ProjectName实例所属项目。default

ChargeInfo

计费类型。

{
"ChargeType": "PrePaid",
"AutoRenew": false,
"PeriodUnit": "Month",
"Period":2,
"Number":1
}

InstanceTags

实例标签。

[{
"Key": "test1",
"Value":"test1"
},{
"Key": "test2",
"Value":"test2"
}]

Number实例购买数量。1

查询 PostgreSQL 实例信息

以下 SDK 请求中所使用的参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail

package rdspostgresql
import (
    "fmt"
    "[github.com/volcengine/volcengine-go-sdk/service/rdspostgresql](http://github.com/volcengine/volcengine-go-sdk/service/rdspostgresql)"
    "testing"
    "[github.com/volcengine/volcengine-go-sdk/volcengine](http://github.com/volcengine/volcengine-go-sdk/volcengine)"
)
func TestRDSPostgreSQL_DescribeDBInstanceDetailCommon(t *testing.T) {
    InitConfig()
    resp, err := Client.DescribeDBInstanceDetail(&rdspostgresql.DescribeDBInstanceDetailInput{
       InstanceId: volcengine.String("postgres-3acf1726****"),
    })
    if err != nil {
       panic(err)
    }
    fmt.Println(resp)
}