Skip to Content

PostgreSQL

Add a PostgreSQL database to an environment, find its connection details, and connect an app to it.

A database is added to one environment of a project, and any app in that environment can connect to it. This guide adds PostgreSQL to the Dev environment and connects an app that is already running there.

Add PostgreSQL

On the project canvas, click + Service, then Database or cache. Pick PostgreSQL.

The Databases list in the add menu, with PostgreSQL
The Databases list in the add menu, with PostgreSQL

The list also has TimescaleDB: PostgreSQL on a TimescaleDB image, for time-series data, with the timescaledb extension already created in the app database. It is the open-source edition ("no compression or continuous aggregates"). It's a separate choice when you add the database: an existing database can't be switched to or from it.

PostgreSQL, TimescaleDB and Valkey in the database list
PostgreSQL, TimescaleDB and Valkey in the database list

A PostgreSQL card appears under Databases marked Ready to deploy. Nothing has been created yet.

The PostgreSQL card on the canvas, Ready to deploy
The PostgreSQL card on the canvas, Ready to deploy

Review and deploy

Click the card. The catalog defaults are PostgreSQL 17, one instance, a 1 GiB volume, the user postgres and the database app. The panel says the database "deploys with the catalog defaults", and that size, high availability and connections are tuned in this panel once it is running. Click Deploy.

Ready to deploy, creates it on Dev right away, with Remove from canvas and Deploy
Ready to deploy, creates it on Dev right away, with Remove from canvas and Deploy

If you change your mind, Remove from canvas takes the card away without creating anything.

When it is ready, the card shows Running.

The PostgreSQL card, Running
The PostgreSQL card, Running

Find the connection details

Click PostgreSQL on the card, open Settings, and look under Access:

  • Connect from apps: the Host, the short Service name, Username, Password and Database. The port is 5432.
  • Connection URL: the same details as a postgresql:// URL, plus a JDBC URL. These work from apps in this environment and are "not reachable from the public internet".
  • Environment snippet: ready-made lines (DATABASE_URL, PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, …) to paste into an app's variables.
Access: Connect from apps, Connection URL and Environment snippet
Access: Connect from apps, Connection URL and Environment snippet

Connect an app

Further down, Connections lists the apps in the environment. Turn on the switch next to your app. The change is staged.

Connections with nginx switched on, marked staged
Connections with nginx switched on, marked staged

Close the panel, click Commit 1 change, then Deploy 1 change. The app redeploys.

1 change to apply: nginx ⇄ postgresql, off → on
1 change to apply: nginx ⇄ postgresql, off → on

Check the app's variables

Open the app and go to Variables. Connecting added:

Variable
PGHOST, PGPORTthe database's in-cluster address (the primary)
PGHOST_RO, PGPORT_ROthe read-only address of the replicas, which only has instances behind it once high availability is on
PGUSER, PGDATABASEthe user and database name
PGPASSWORDread from the database's secret ("Provided by add-on")
The app's variables, with PGHOST to PGDATABASE and PGPASSWORD provided by add-on
The app's variables, with PGHOST to PGDATABASE and PGPASSWORD provided by add-on

Connecting also adds DATABASE_URL, a postgres:// URL built from the PG* values. A variable you set yourself with the same name always wins.

When the database includes its own connection pooler (some templates add one), connecting also adds PGHOST_POOLER and PGPORT_POOLER, the pooler's address.

Build steps and the Pre-deploy command get the same variables. When the service has no usable DATABASE_URL of its own, they get one built from the PG* values, so a migration that reads DATABASE_URL works at build time too.

If the app is connected to two databases that both set DATABASE_URL (for example PostgreSQL and MariaDB), the first keeps the plain names and the second also gets its whole set under its own prefix, such as MARIADB_DATABASE_URL and MARIADB_MYSQL_HOST.

From the connected app, psql "$DATABASE_URL" reached the database:

$ psql "$DATABASE_URL" -At -c "show server_version;" -c "select current_database();"
17.5 (Debian 17.5-1.pgdg110+1)
app

To disconnect, turn the switch off and deploy the change; the PG* variables are removed from the app.

The database card

The card on the canvas shows the database's address for the SQL console (see below) and its status. The instance count (1 with an arrow) opens the list of the database's volumes, with their size.

The PostgreSQL card with its volume list open: postgresql-…-1, 1Gi
The PostgreSQL card with its volume list open: postgresql-…-1, 1Gi

Connect from your machine

stackblaze connect postgres opens a psql shell on the database without opening it to the internet. Name the app it's connected to:

stackblaze connect postgres -p shop -e dev -a web            # psql runs inside the app
stackblaze connect postgres --local -p shop -e dev -a web    # psql runs on your machine
stackblaze connect postgres --tunnel -p shop -e dev -a web   # keep a local port open for GUI tools
  • By default psql runs inside the app's instance.
  • --local runs your own psql through a private tunnel: Tunnel 127.0.0.1:33567 → postgres addon "postgresql-u0kr". Running local psql…
  • --tunnel keeps the tunnel open and prints the host, port, user and database for a GUI tool. --print-only prints a postgresql://…@127.0.0.1:<port>/… connection string instead of starting a client, and --port picks the local port.

The tunnel listens on 127.0.0.1 only, closes after 30 minutes idle, and needs write access to the app. When the app has several databases, add --instance <name>.

Connect from outside StackBlaze

By default the database is only reachable from apps in the same environment. Public access opens it to clients outside the platform (a laptop, a BI tool, another cloud), only from IP addresses you allow, and only over TLS.

Allow your IP addresses

Open the database's Settings → Access and find Public access. In Allowed IPs, enter the addresses that may connect, separated by commas. Ranges in CIDR form (like 198.51.100.0/24) work too.

Allow any IP lets anyone who has the password connect. The panel marks it "not recommended".

Public access, Off, with an allowed IP entered
Public access, Off, with an allowed IP entered

Turn it on

Turn on the Public access switch. You get an Address and a Connection string that ends in ?sslmode=require.

Public access, On, with Address, Connection string and Allowed IPs
Public access, On, with Address, Connection string and Allowed IPs

Connect

Copy the Connection string and use it from an allowed address, for example:

psql "postgresql://postgres:<password>@<address>/app?sslmode=require"

When we did this, the address refused connections for a short while after the switch turned on, then accepted them.

To change the list later, edit Allowed IPs and click Save allowed IPs. Turning the switch off asks "Turn off public access? Clients outside the platform will be disconnected." After that, the address no longer accepts connections.

SQL console

The address on the database card (db.…) opens a SQL console in a new tab. You are signed in, and the database is listed under Shared.

The SQL console with the project's database under Shared
The SQL console with the project's database under Shared

Open a SQL editor, choose the database's connection if the editor asks for one, type a query and press Ctrl+Enter.

A query in the SQL editor and its result
A query in the SQL editor and its result

The Database console page covers the rest: the navigator, scripts, the execution plan, editing rows and exporting results.

Extensions

Settings → Extensions lists the PostgreSQL extensions available on the database's image and how many are enabled. Extensions are per database: pick the database (for example app) in the menu next to Refresh. Use Filter by name… to find one, then turn its switch on.

Extensions, filtered to pg_trgm, switched On
Extensions, filtered to pg_trgm, switched On

Some extensions are marked needs restart: they load a library into shared_preload_libraries.

pg_stat_statements, marked needs restart, Off
pg_stat_statements, marked needs restart, Off

Turning one on asks first. The dialog warns that it "triggers a rolling restart of the PostgreSQL cluster. Existing connections will be briefly interrupted while the primary restarts." Click Restart & enable.

Enable pg_stat_statements: needs its library added to shared_preload_libraries, with Cancel and Restart & enable
Enable pg_stat_statements: needs its library added to shared_preload_libraries, with Cancel and Restart & enable
pg_stat_statements, On
pg_stat_statements, On

Storage

Settings → Storage shows each volume, how much of it is used, and Expand storage. Volumes can only grow.

Storage: the volume's usage and Expand storage
Storage: the volume's usage and Expand storage

Choose a new size and click Expand.

Expand storage with the size options open
Expand storage with the size options open

The resize can take a few minutes. Once it is done, the volume shows its new size.

Storage after expanding: 10 GiB
Storage after expanding: 10 GiB

High availability

A new database runs as a Single primary. Settings → HA → Enable HA turns it into a cluster of three.

High availability: Single primary, with Enable HA
High availability: Single primary, with Enable HA

The dialog says it "Scales the Postgres cluster to 3 instances. This is a one-way upgrade — there is no in-place downgrade to a single primary." Click Enable HA.

Enable high availability? with Cancel and Enable HA
Enable high availability? with Cancel and Enable HA

When it is done, the section shows the cluster: its health, 3/3 replicas ready, which instance is the primary, the mode (Streaming replication), and each member with its role (one primary, two replica).

High availability: 3 instances, Healthy, one primary and two replicas
High availability: 3 instances, Healthy, one primary and two replicas

Connected apps keep the same variables: PGHOST still points at the primary, and PGHOST_RO now reaches the two replicas, so you can send read-only queries there.

Deployments

The Deployment tab lists the database's deploys and restarts, with the image each one ran.

Deployments: Deploy running, with the image and date
Deployments: Deploy running, with the image and date

The ⋯ menu on a deploy has Redeploy and Inspect Deployment.

The menu on a deploy: Redeploy, Inspect Deployment
The menu on a deploy: Redeploy, Inspect Deployment

Inspect Deployment opens that deploy's logs.

Inspect Deployment: the deploy's logs
Inspect Deployment: the deploy's logs

Redeploy asks first ("This will restart the add-on with the exact same configuration"), then restarts the database. The deploy's time in the list changes to when it restarted.

Redeploy deployment confirmation
Redeploy deployment confirmation
The Deployment tab after a redeploy
The Deployment tab after a redeploy

Logs

The Logs tab streams the database's logs.

The Logs tab
The Logs tab
  • All pods: show every instance, or pick one (for example primary · postgresql-…-1).
  • App logs / System events: the database's own output, or platform events about it.
  • Search logs: filter the lines.
  • Live tail, Last 15 minutes, Last 1 hour, Last 6 hours, Last 24 hours, All loaded: the time range.
  • The expand button opens the logs full size. The ⋯ menu has Refresh, Copy logs and Download logs.
The time range menu: Live tail to All loaded
The time range menu: Live tail to All loaded
The source menu: App logs, System events
The source menu: App logs, System events

Metrics

The Metrics tab shows the last 2h, 24h or 7d, with Refresh. At the top: Connections (open sessions), Queries (statement rate), Replica lag and Data size.

Metrics: the summary and Host resources
Metrics: the summary and Host resources

Below that are charts in four groups:

GroupCharts
Host resourcesMemory usage, CPU usage
WorkloadOpen connections, Queries / s, Writes / s (each Overall or Per-pod)
HealthQuery time, Waiting on locks
Storage & replicasData on disk, Replica lag
Metrics: Workload charts
Metrics: Workload charts
Metrics: Health charts
Metrics: Health charts

Plan limits

The top of Settings shows what your plan allows for this database: how many clients can connect at once (Connections) and how long its backups are kept (Backup retention).

Plan limits: Connections and Backup retention
Plan limits: Connections and Backup retention

Import a dump

To load an existing database, see Import a database dump.

Backups

Back up the database, restore it as a copy, and move your apps onto the copy with Switch instance: see Backups and restore. A recovery point holds a dump of the database.

PostgreSQL also has point-in-time recovery, through the API only.

Delete

Delete add-on, at the bottom of Settings, deletes the database and its data. It is only available once no apps are connected: disconnect them first in Connections.

On this page