You need to enable JavaScript to run this app.
RDS for PostgreSQL

RDS for PostgreSQL

Copy page
Download PDF
V2(2022-01-01)
Using Go SDK
Copy page
Download PDF
Using Go SDK
This topic describes the download, installation, and client initialization related information of the Go SDK for RDS for PostgreSQL.​
Download and install​
Prerequisites​
Go 1.5 or above is required, and Go 1.12 or above is recommended.​
tip​
You can check the current Go version information by executing the go version command.​
SDK download URL​
The source code address of the Go SDK for RDS for PostgreSQL, see SDK list.​
Install SDK​
Execute the following command under the target Go project to install the Go SDK.​
​
go get github.com/volcengine/volcengine-go-sdk/volcengine/credentials​
go get github.com/volcengine/volcengine-go-sdk/volcengine/volcengineutil​
​
Initialize client​
The RDS for PostgreSQL Go SDK supports initializing clients through Session. The PostgreSQL client created through Session implements reuse of HTTP/HTTPS resources (connections or requests) at the underlying level. When multiple clients need to be initialized for operations, creating them via Session can improve client performance.​
Prerequisites​
  • The Access Key ID (AK) and Secret Access Key (SK) information of the account have been obtained. For specific operations, refer to Access key management.​
  • The supported Regions and corresponding Hosts of RDS for PostgreSQL have been obtained. Please refer to service address.​
  • The RDS for PostgreSQL Go SDK has been downloaded and installed. For specific steps, see Download and Install.​
Initialize client​
Initialize the client with the following code.​
​
package rdspostgresql​
import (​
"github.com/volcengine/volcengine-go-sdk/volcengine"​
"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"​
"github.com/volcengine/volcengine-go-sdk/volcengine/session"​
"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 Host"​
)​
func InitConfig() {​
Config = volcengine.NewConfig().​
WithCredentials(credentials.NewStaticCredentials(AK, SK, "")).​
WithRegion(REGION).WithEndpoint(ENDPOINT)​
Sess, _ = session.NewSession(Config)​
Client = rdspostgresql.New(Sess)​
}​
​
Sample​
Request timed out time settings​
When using the Go SDK to call the PostgreSQL interface, there is no timeout set by default. You can specify the client's request timeout when initializing the client by using WithHTTPClient(&http.Client{Timeout: <timeout> * time.Second}). If the request is not completed within this time, it will be immediately terminated and report a timeout error. For example, you can set the client's request timeout to 10 seconds with the following code.​
​
config = volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(ak, sk, "")).WithRegion(region).WithHTTPClient(&http.Client{Timeout: 10 * time.Second})​
​
Creating PostgreSQL instance​
For the definitions, value ranges, and other information of the parameters used in the following SDK code, please refer to CreateDBInstance.​
​
package rdspostgresql​
import (​
"fmt"​
"github.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)​
}​
​
The parameters in this example are described in the following table.​
​
Configuration item​
Note​
Value​
DBEngineVersion​
Compatible version.​
PostgreSQL_12​
NodeInfo​
Instance specification configuration.​
[​
{​
"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​
Storage type.​
LocalSSD​
StorageSpace​
Instance storage space.​
30​
VpcId​
A proprietary network (VPC) ID.​
vpc-2bzo8w6m87v282dx0eemg****​
SubnetId​
Subnet ID.​
subnet-2d6b6byrhtts058ozfenq****​
InstanceName​
Storage type.​
pg-test-sdk-go​
ProjectName​
Affiliated project of the instance.​
default​
ChargeInfo​
Billing method.​
{​
"ChargeType": "PrePaid",​
"AutoRenew": false,​
"PeriodUnit": "Month",​
"Period":2,​
"Number":1​
}​
InstanceTags​
Instance tag.​
[{​
"Key": "test1",​
"Value":"test1"​
},{​
"Key": "test2",​
"Value":"test2"​
}]​
Number​
Instance purchase quantity.​
1​
​
Query PostgreSQL instance information​
For information about the definition, range, and other parameters used in the following SDK requests, see DescribeDBInstanceDetail.​
​
package rdspostgresql​
import (​
"fmt"​
"github.com/volcengine/volcengine-go-sdk/service/rdspostgresql"​
"testing"​
"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)​
}​
​
​
Last updated: 2025.11.17 20:31:12