Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 35 ยท Storage and Databases

DigitalOcean Managed Databases: PostgreSQL, MySQL, Valkey and More

Run PostgreSQL, MySQL, Valkey, MongoDB, Kafka or OpenSearch as a managed service: clusters, standby nodes, trusted sources, private hostnames, TLS connection strings and connection pools.

Installing a database on a Droplet is easy. Running it well is not: you must patch it, back it up, monitor disk space, handle failover and tune it. A managed database hands those chores to DigitalOcean. You get a cluster with an endpoint, TLS certificates, automated backups and updates, and you connect with a normal connection string.

Engines available

DigitalOcean offers these managed engines (availability can vary by region and over time):

  • PostgreSQL: the general-purpose relational default, with extensions and read-only nodes.
  • MySQL: the other major relational engine, popular with PHP and WordPress.
  • Valkey (the open-source, Redis-compatible in-memory store): caches, sessions, queues, rate limits. Earlier versions of the product were called Redis.
  • MongoDB: document database.
  • Kafka: managed event streaming.
  • OpenSearch: search and log analytics.

If you want to learn the databases themselves, see PostgreSQL, MySQL and MongoDB on this site; here we focus on the platform side.

Cluster anatomy

A cluster is made of nodes. You choose:

  • the engine and version;
  • the region and VPC (put it in the same region and VPC as your app);
  • the node size and count: one node is cheapest but a failure means downtime; adding standby nodes gives automatic failover; read-only nodes (replicas) spread read traffic;
  • storage, which grows with the plan (some engines allow adding disk without changing the size).

For production, use a plan with at least one standby. For development, a single small node is fine.

flowchart LR
  APP["App servers<br/>(VPC, same region)"] -- "writes" --> P[("Primary node")]
  APP -- "reads" --> R[("Read-only node")]
  P -- "replicates" --> R
  P -- "replicates" --> S[("Standby node")]
  S -. "promoted on failure" .-> P

A primary with a standby for failover and an optional replica for read scaling.

Creating a cluster

Terminal
doctl databases create shop-db --engine pg --version 16 \
  --region fra1 --size db-s-1vcpu-2gb --num-nodes 2 --private-network-uuid <vpc-id>
doctl databases list --format ID,Name,Engine,Version,Status
doctl databases connection <cluster-id> --format Host,Port,User,Database

After a few minutes the cluster is online. By default it contains an administrative user (doadmin) and a default database (defaultdb). Create your own database and a user with fewer privileges for each application instead of using doadmin in app code:

Terminal
doctl databases db create <cluster-id> shop
doctl databases user create <cluster-id> shop_app

Connecting securely

Connection details come in two flavours:

  • A public hostname such as shop-db-do-user-123-0.b.db.ondigitalocean.com, reachable from anywhere (subject to trusted sources).
  • A private hostname, the same name with a private- prefix, resolving to an address inside your VPC. Use this from your Droplets.

Ports are not the default ones: a PostgreSQL cluster typically listens on 25060, and connection pools on 25061. Always check the connection details in the panel. TLS is required: use sslmode=require (or verify-full with the CA certificate you download from the panel).

A connection string has this shape (placeholders only):

TEXT
postgresql://shop_app:<password>@private-shop-db-do-user-123-0.b.db.ondigitalocean.com:25060/shop?sslmode=require

Keep it in an environment variable (for example DATABASE_URL), never in source control.

Trusted sources: the database's own firewall

A managed database accepts connections only from its trusted sources list. Entries can be a Droplet, a tag, a Kubernetes cluster, an App Platform app or an IP/CIDR. An empty list used to mean "open to everyone"; since that is dangerous, add your sources immediately and keep it tight.

Terminal
doctl databases firewalls append <cluster-id> --rule tag:web
doctl databases firewalls append <cluster-id> --rule ip_addr:203.0.113.7/32
doctl databases firewalls list <cluster-id>

Use tags (the same web tag as in Cloud Firewalls) so new app servers are covered automatically.

Connection pools

Each PostgreSQL connection costs memory, and a cluster allows only a limited number. With many app servers, each opening many connections, you can run out. A connection pool (PgBouncer, built into the managed service) keeps a small set of real connections and shares them among many clients. You create pools per database and user, choose a pool mode (transaction mode is the usual best choice) and size, and connect to the pool's port instead.

Sizing rule of thumb: total pool size across all pools should stay below the cluster's connection limit, leaving a few connections for admin tools. Transaction mode does not support session features such as SET values that must persist or advisory locks held across transactions, so test your application.

Python Stand-in: parse and build connection strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The password in the example is fake and contains special characters on purpose: reserved characters such as @ and / must be percent-encoded in URLs, a frequent cause of "cannot connect" errors.

Python Stand-in: size connection pools and check trusted sources

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Operations you get (and what you still own)

Included: daily backups with point-in-time recovery on supported engines, automatic minor-version patching in a maintenance window you choose, monitoring graphs, alerts, failover, TLS certificates.

Still your job: schema design and migrations, indexes and slow queries, creating least-privilege users, choosing sizes, scaling up before you run out of disk or connections, application-level backups for off-site copies (Snapshots and backups), and testing failover behavior in your application: a failover drops connections, so your code needs retry logic.

Major version upgrades are your decision: they are done by forking or upgrading the cluster at a planned time, after testing on a copy created from a backup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which hostname should a Droplet in the same VPC use to reach a managed database?

    The private hostname resolves to an address inside the VPC, so traffic stays on the free private network.
  2. 2. What are 'trusted sources' on a managed database?

    They act as the database's own firewall.
  3. 3. Why use a connection pool?

    Each connection costs memory; pooling prevents exhausting the cluster's connection limit.
  4. 4. Which is still YOUR responsibility with a managed database?

    The provider operates the server; you own how your data and queries use it.

Summary

  • Managed databases give you PostgreSQL, MySQL, Valkey, MongoDB, Kafka and OpenSearch with backups, patching, standby nodes and TLS.
  • Place the cluster in your VPC, connect via the private- hostname, require TLS and keep the URL in an environment variable.
  • Lock access with trusted sources (preferably tags), use app-specific users, and add connection pools to protect connection limits.
  • Plan for failover in application code and keep your own off-site backups.

Next: App Platform: Deploy Apps Without Servers