# Managed PostgreSQL

Run PostgreSQL on dedicated CPUs with local NVMe, without a server to look after. We take care of provisioning, failover and patch updates, and every database comes with PgBouncer connection pooling built in.

## Create a PostgreSQL database

1. Go to **Databases** and click **Create database**.
2. Select **PostgreSQL**.
3. Enter a **Name**. It must be unique in your organization.
4. Select the **Data center** your servers run in. See [Regions](https://betterstack.com/docs/cloud/servers/regions/#choose-a-region).
5. Select a **Plan**.
6. Under **Nodes**, select **Primary + 1 replica**, so a replica takes over if the primary fails.
7. Turn on **Allow public access** if clients outside your private networks connect to the database.
8. Click **Create database**.

The database is ready when its status reads **Running**. Replicas join the **Nodes** list after that, one at a time, as each one copies the primary.

![Create database form with PostgreSQL selected](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/748b3ffc-db96-43c8-682a-50bd3ffee800/md2x =2180x2156)

## Plans

| Plan | vCPU and RAM | Storage | Direct connections | Pooled connections |
|------|--------------|---------|--------------------|--------------------|
| PG1 | 4 vCPU, 4 GB | 64 GB | 100 | 400 |
| PG2 | 8 vCPU, 8 GB | 128 GB | 200 | 800 |
| PG3 | 8 vCPU, 32 GB | 192 GB | 800 | 3,200 |
| PG4 | 16 vCPU, 64 GB | 256 GB | 1,600 | 6,400 |
| PG5 | 32 vCPU, 120 GB | 512 GB | 3,000 | 12,000 |
| PG6 | 48 vCPU, 160 GB | 768 GB | 4,000 | 16,000 |
| PG7 | 64 vCPU, 192 GB | 1,024 GB | 4,800 | 19,200 |
| PG8 | 128 vCPU, 480 GB | 1,536 GB | 5,000 | 48,000 |

Every node gets the full plan and bills at the plan's price, so a primary with one replica costs twice the plan. Storage is what your data can use. We keep extra disk for the write-ahead log and temporary files. Traffic over private networks, replication included, is free. See [Pricing philosophy](https://betterstack.com/docs/cloud/pricing/#network-and-ip-address-prices) for internet egress.

## Connect to your database

Open the database and find the hostname, the `defaultdb` database, the `admin` user and its password under **Connection**. Click **Copy database URL** for a ready-made connection string and set it as `DATABASE_URL`. The URL verifies the server's certificate, which is issued by the Better Stack Managed CA. Save the CA certificate from `ca_certificate` in `betterstack postgres credentials <name> --availability-zone us-west-2a` as `betterstack-db-ca.pem`, and connect:

```bash
[label Connect with psql]
psql "$DATABASE_URL&sslrootcert=betterstack-db-ca.pem"
```

The database listens on two ports:

- **5432** connects directly. Use it for migrations, `pg_dump` and `pg_restore`, and anything that keeps session state.
- **6432** goes through PgBouncer in transaction mode. Use it for apps that open many connections. Session state doesn't carry across transactions there, so `LISTEN` and `NOTIFY`, `SET` and session advisory locks need port 5432. Prepared statements work on both.

A new database is reachable only from the [private networks](https://betterstack.com/docs/cloud/networks/private-networks/) attached to it. The hostname always points at the current primary, so clients follow a failover by reconnecting.

**Allow public access** opens both ports on a separate public hostname, and public connections must use TLS. Get it with `betterstack postgres credentials <name> --availability-zone us-west-2a`, in `public_uri` and `public_pooled_uri`. Restrict who can connect with a [firewall](https://betterstack.com/docs/cloud/networks/firewalls/). A firewall filters only the public address, not traffic from private networks. While public access is on, port 6432 requires TLS from private clients too, so give them `sslmode=require`.

[info]
#### Connection hangs from outside Cloud?
The hostname resolves to a private address, so it works only from your private networks. From the internet, turn on public access and connect to the public hostname.
[/info]

## Rotate credentials

To replace the `admin` password:

1. Click **Rotate credentials** on the database page.
2. Click **Rotate credentials** to confirm.

The old password stops working for new connections right away, while open connections stay up, so plan the client update before you rotate.

## Users and extensions

Every database comes with the `defaultdb` database and the `admin` user. Extra users and databases can't be created with SQL. Create them with the [Cloud CLI](https://betterstack.com/docs/cloud/getting-started/cli/) or the API instead: [add a database](https://betterstack.com/docs/cloud/api/postgres-databases/create-postgres-database/), then [add a user](https://betterstack.com/docs/cloud/api/postgres-users/create-postgres-user/) with `none`, `read`, `read_write` or `all` access to each database. `admin` has full access to every database and can read query statistics and cancel queries. No user has superuser rights, so `ALTER SYSTEM` and server file access aren't available.

Install extensions yourself with `CREATE EXTENSION`:

```sql
[label Enable pgvector]
CREATE EXTENSION vector;
```

Available extensions:

- `vector` for embeddings and similarity search.
- `postgis`, `postgis_topology` and `postgis_raster` for geospatial data.
- `pg_partman` for partition management.
- `hypopg` for hypothetical indexes.
- The PostgreSQL contrib modules, such as `pgcrypto`, `pg_trgm`, `uuid-ossp`, `hstore`, `citext`, `btree_gin` and `btree_gist`. `pg_stat_statements` is preloaded, so `CREATE EXTENSION pg_stat_statements` is all it takes.

Other extensions, such as `timescaledb`, `pg_cron`, `pgaudit` and `pg_repack`, aren't available.

## Replicas and failover

Replicas run on separate physical hosts in the same data center and copy the primary asynchronously. They serve read-only traffic.

With at least one replica, failover is automatic. When the primary is unreachable for about 90 seconds, the most caught-up replica becomes the primary and the hostname moves to it, usually within 3 minutes.

Because replication is asynchronous, writes that hadn't reached that replica yet are lost. The old primary comes back as a replica. A database without replicas has no failover and is down until its node recovers.

To switch on purpose, for a disaster recovery drill or a planned move:

1. Click **Fail over** on the database page.
2. Click **Fail over** to confirm.

The primary hands over cleanly with no committed write lost, and writes fail for about 20 to 30 seconds while clients reconnect.

[warning]
#### Does your client cache DNS?
The hostname moves to the new primary with a 60-second TTL. Make your client re-resolve the hostname and retry with backoff on connection errors, rather than pinning an IP address or caching DNS for longer.
[/warning]

## Snapshots

Click **Take a snapshot** on the database page. The database keeps serving while the snapshot is taken, and every committed transaction is in it. Snapshots outlive the database. To restore one, see [Database snapshots](https://betterstack.com/docs/cloud/databases/snapshots/#roll-a-database-back).

## Rescale and disk usage

To change the plan:

1. Click **Rescale** on the database page.
2. Select a **Plan** and click **Rescale database**.

With replicas, nodes resize one at a time and clients see a single reconnect. A database without replicas is unavailable for a few minutes. You can move to a smaller plan only if your data fits within 80% of its storage.

When storage fills up, the database protects itself:

| Storage used | What happens |
|--------------|--------------|
| 80% | We send a disk usage warning to your [webhooks](https://betterstack.com/docs/cloud/integrations/webhooks/). |
| 90% | The database turns **Degraded**. |
| 95% | New transactions are read-only, so you can still connect and delete data. |

The database turns writable again once usage drops below 90%. To delete data while it's read-only, turn read-only off inside the transaction:

```sql
[label Delete old rows while the database is read-only]
BEGIN;
SET transaction_read_only = off;
DELETE FROM big_table WHERE created_at < now() - interval '30 days';
COMMIT;
```

`DELETE` alone doesn't free disk space. Run `VACUUM FULL` on the table you emptied, in a session with read-only turned off:

```sql
[label Reclaim the space]
SET default_transaction_read_only = off;
VACUUM FULL big_table;
```

## Updates

We apply PostgreSQL minor releases automatically. Replicas restart first, then the primary restarts for a few seconds.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
