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
- Go to Databases and click Create database.
- Select PostgreSQL.
- Enter a Name. It must be unique in your organization.
- Select the Data center your servers run in. See Regions.
- Select a Plan.
- Under Nodes, select Primary + 1 replica, so a replica takes over if the primary fails.
- Turn on Allow public access if clients outside your private networks connect to the database.
- 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.
Plans
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 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:
The database listens on two ports:
- 5432 connects directly. Use it for migrations,
pg_dumpandpg_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
LISTENandNOTIFY,SETand session advisory locks need port 5432. Prepared statements work on both.
A new database is reachable only from the 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. 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.
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.
Rotate credentials
To replace the admin password:
- Click Rotate credentials on the database page.
- 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 or the API instead: add a database, then add a 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:
Available extensions:
vectorfor embeddings and similarity search.postgis,postgis_topologyandpostgis_rasterfor geospatial data.pg_partmanfor partition management.hypopgfor hypothetical indexes.- The PostgreSQL contrib modules, such as
pgcrypto,pg_trgm,uuid-ossp,hstore,citext,btree_ginandbtree_gist.pg_stat_statementsis preloaded, soCREATE EXTENSION pg_stat_statementsis 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:
- Click Fail over on the database page.
- 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.
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.
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.
Rescale and disk usage
To change the plan:
- Click Rescale on the database page.
- 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:
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:
DELETE alone doesn't free disk space. Run VACUUM FULL on the table you emptied, in a session with read-only turned off:
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! 🙏