Databases are exposed by the Databases interface embedded in rest.Rest. They are available on Standard, Pro and Enterprise plans.
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()
api := rest.New(client)
Creating a database
api.CreateDatabase(opts) provisions a new database from a squarecloud.DatabaseCreateOptions.
created, err := api.CreateDatabase(squarecloud.DatabaseCreateOptions{
Name: "my-mongo",
Memory: 1024,
Type: squarecloud.DatabaseTypeMongo,
Version: "8",
})
if err != nil {
panic(err)
}
fmt.Println(created.ID)
fmt.Println(created.ConnectionURL) // ready to use — password already embedded
fmt.Println(created.Password) // shown once — store securely
fmt.Println(created.Certificate) // shown once — only for engines that issue a TLS cert
| Field | Type | Description |
|---|
Name | string | Display name, 1–32 characters |
Memory | int | Allocated memory in MB |
Type | squarecloud.DatabaseType | Engine: DatabaseTypeMongo, DatabaseTypeMySQL, DatabaseTypeRedis or DatabaseTypePostgres (slugs mongo, mysql, redis, postgres) |
Version | string | Engine version |
Password (and Certificate, when applicable) appear only in the creation response and cannot be recovered later — store them now. ConnectionURL comes ready to use with the password embedded.
An unsupported type fails with INVALID_DATABASE_TYPE; too little memory for the plan fails with INSUFFICIENT_MEMORY; a failure during provisioning fails with DATABASE_CREATION_FAILED.
Listing your databases
api.GetDatabases() lists every database you own as compact squarecloud.UserDatabase descriptors:
dbs, err := api.GetDatabases()
if err != nil {
panic(err)
}
for _, db := range dbs {
fmt.Printf("%s (%s)\n", db.Name, db.ID)
}
Fetching a single database
db, err := api.GetDatabase("abc123def456abc123def456")
if err != nil {
panic(err)
}
fmt.Println(db.ID, db.Name, db.Type, db.RAM, db.Cluster, db.CreatedAt)
Lifecycle
err := api.StartDatabase("abc123def456abc123def456")
if err != nil {
panic(err)
}
err = api.StopDatabase("abc123def456abc123def456")
Status and metrics
The database status and metrics endpoints share the same shapes as the application ones (they are type aliases) — see Managing applications for the field-by-field breakdown.
status, err := api.GetDatabaseStatus("abc123def456abc123def456")
if err != nil {
panic(err)
}
fmt.Println(status.Status, status.CPU, status.RAM) // formatted strings
raw, err := api.GetDatabaseStatusRaw("abc123def456abc123def456") // numeric values
if err != nil {
panic(err)
}
metrics, err := api.GetDatabaseMetrics("abc123def456abc123def456")
if err != nil {
panic(err)
}
fmt.Println(len(metrics), "metric points (up to 288 over 24h)")
Summary status for every database
list, err := api.GetDatabaseListStatus()
if err != nil {
panic(err)
}
for _, summary := range list {
fmt.Printf("%s → running=%v\n", summary.ID, summary.Running)
}
Updating
api.UpdateDatabase(dbID, opts) changes the display name and/or memory allocation. At least one field must be provided.
err := api.UpdateDatabase("abc123def456abc123def456", squarecloud.DatabaseUpdateOptions{
Name: "primary-db",
RAM: 1024,
})
if err != nil {
panic(err)
}
Credentials
api.GetDatabaseCertificate(dbID) returns the TLS certificate (base64-encoded PEM):
cert, err := api.GetDatabaseCertificate("abc123def456abc123def456")
if err != nil {
panic(err)
}
fmt.Println("certificate length:", len(cert))
api.ResetDatabaseCredentials(dbID, reset) rotates either the password or the certificate, using the squarecloud.DatabaseReset* constants:
// Rotate the password — the new value is shown only once
result, err := api.ResetDatabaseCredentials("abc123def456abc123def456", squarecloud.DatabaseResetPassword)
if err != nil {
panic(err)
}
fmt.Println("new password:", result.Password)
// Rotate the certificate — fetch the new one via GetDatabaseCertificate
_, err = api.ResetDatabaseCredentials("abc123def456abc123def456", squarecloud.DatabaseResetCertificate)
The new password is returned only on the password reset response and displayed a single time — store it immediately.
Snapshots
Database snapshots mirror the application snapshot API — see Snapshots.
snapshots, err := api.GetDatabaseSnapshots("abc123def456abc123def456")
if err != nil {
panic(err)
}
created, err := api.CreateDatabaseSnapshot("abc123def456abc123def456")
if err != nil {
panic(err)
}
fmt.Println("download URL:", created.URL)
Deleting a database
Deleting a database is irreversible. Make sure you have a recent snapshot if you might need to recover the data.
err := api.DeleteDatabase("abc123def456abc123def456")
if err != nil {
panic(err)
}