# Get started with Managed PostgreSQL

Create a single-node database, read its password, connect with psql over TLS, and delete it again, in about ten minutes.

Replace _orders_ with a name of your own and _ewdb-0123456789ab_ with the id the create returns.

## Prerequisites

- Permission for `rds:CreateDBInstance`, `rds:DescribeDBInstances` and `rds:DeleteDBInstance`,
  and `secrets:GetSecretValue` to read the password.
- For the CLI steps, the dcloud CLI, signed in. For the connection, `psql` on your computer.

## Step 1: Create a database

A single node on a `performance-2`, reachable from the internet so that you can connect from your
computer. You pay for the database from now on.

**Console**
1. Open **Postgres**, then choose **Create Postgres database**.
2. For **Name**, enter orders.
3. Under **Size**, for **Nodes**, choose **Single node**.
4. For **Machine type**, choose `performance-2`.
5. Under **Security**, select **Reachable from the internet**.
6. Choose **Create database**.

The database page opens with the status `creating`.

**CLI**
```sh
dcloud rds instances create --identifier orders --instance-class performance-2 --publicly-accessible
```

**API**
```http
POST /rds/api/instances
{ "identifier": "orders", "instanceClass": "performance-2", "publiclyAccessible": true }
```

**Terraform**
```hcl
resource "dcloud_db_instance" "orders" {
  identifier          = "orders"
  instance_class      = "performance-2"
  publicly_accessible = true
}
```

The status changes from `creating` to `available` within a few minutes. Each step appears on the
database's **Activity**.

## Step 2: Read the master password

**Console**
1. On the database page, choose **Connection**.
2. Under **Master password**, choose **Reveal**.

**CLI**
The database's `masterSecretId` names the secret:

```sh
dcloud rds instances get ewdb-0123456789ab
dcloud secrets secrets get-value master-secret-id
```

**API**
```http
GET /rds/api/instances/ewdb-0123456789ab
```

Then read the value of the secret named by `masterSecretId` with
`GET /secrets/api/secrets/{masterSecretId}/value`.

**Terraform**
`dcloud_db_instance.orders.master_secret_id` names the secret that holds the password.

Reading a secret value is recorded in the audit log.

## Step 3: Connect

The **Connection** tab shows the connection strings. The CLI downloads the platform's CA and runs
`psql` with it, so `verify-full` checks that you reach your own database:

```bash
dcloud rds exec -- psql "postgresql://admin:<password>@<endpoint>:6432/appdb?sslmode=verify-full"
```

`admin` and `appdb` are the defaults for the master user and the first database. Run
`SELECT version();` to see the server answer.

## Clean up

Delete the database so that you stop paying for it.

**Console**
1. On the database page, choose **Danger zone**.
2. Under **Delete this database**, clear **Take a final backup and keep the backups for N days**.
3. Choose **Delete database**.
4. Enter the database name, and choose **Delete database**.

**CLI**
```sh
dcloud rds instances delete ewdb-0123456789ab
```

**API**
```http
DELETE /rds/api/instances/ewdb-0123456789ab
```

**Terraform**
```sh
terraform destroy
```

The machine, its volume, its address and its backups are removed.

## Next steps

- [Connect to a database](connect.md) from an application.
- [Create a database](create-a-database.md) as a cluster, in a private network.
- [Instances, clusters and endpoints](instances-and-clusters.md)
