Not yet released. This guide describes implemented changes awaiting rollout. Availability requires the corresponding backend and dashboard release.
Goal
Let a quiet database sleep, and set up your app so the first connection after a sleep just works.Prerequisites
- A PostgreSQL database that can sleep (see What can’t sleep)
- An app whose database driver can retry a connection
Workflow
1
Turn it on in the database’s Settings, under Size, then Sleep when idle: pick a timer from 5 minutes to 24 hours and save. From the CLI, run
stackshift db sleep shop-db --after 15m. Branches sleep after 5 minutes without any setup. Your main database stays awake until you turn this on.2
Set your driver’s pool as described in Driver settings: a health check or pre-ping, and an idle timeout shorter than your sleep timer.
3
Check it. Once the timer has passed without traffic the database shows Sleeping. Connect again and the first connection takes a moment longer, then it works as usual.
4
Before a batch job, wake the database yourself with
stackshift db wake shop-db --wait.When a database counts as idle
Idle means no data went either way on any connection to the database, whether private, Shared, public or through the tunnel, for the whole timer. StackShift’s own health checks and dashboard views don’t count as use and never wake it. An app that polls the database every minute keeps it awake.How waking works
- The first connection after a sleep is held while the database starts. Your client sees a slower connect, not an error.
- As a target, a Starter database wakes in about a second and a Dedicated database in a few seconds. These are targets, not promises.
- Connections that arrive together are all held, up to 200 per database. Beyond that, and if the database hasn’t woken in time (30 seconds, or 60 on Dedicated), a connection gets the error below.
- The address, password and firewall rules stay in place while the database sleeps. Nothing changes for your app.
Shared connections and direct connections
- Shared connections (pooled) on Starter survive sleep. Your app’s connections stay open, the first query waits for the database to wake and then runs, and nothing disconnects. If the wake fails, only that query gets the error and the connection stays open.
- Direct connections are closed when the database goes to sleep. On Dedicated databases, Shared connections close at sleep too.
- Branches use Shared connections by default. Keep migrations and admin tools on the direct address, and let them reconnect.
The “database is waking up” error
If the database can’t wake in time, the connection is answered with PostgreSQL error57P03, “the database is waking up, try again in a few seconds”. Most drivers and ORMs treat this as retryable. Retry after a couple of seconds, a few times.
With sslmode=verify-full you still get this real error, not “server closed the connection”, because the answer is given with the database’s own certificate. Clients that skip TLS get the same error in plain text.
Retry while it wakes
Driver settings
Two settings matter for every driver. First, a health check or pre-ping, so a connection that was closed during sleep is replaced instead of failing your request. Second, an idle timeout below your sleep timer, so your pool lets go of idle connections before the database does. The shortest timer is 5 minutes, so 2 minutes is safe everywhere. Also allow a connect timeout of at least 30 seconds. For node-postgres, setidleTimeoutMillis to 120000 and connectionTimeoutMillis to 30000, and add pool.on("error", (error) => console.error(error)) so a connection closed in the background is logged instead of crashing your process. The other drivers are below.
What can’t sleep
- Databases with a standby or read copies, and databases that are themselves a standby or a copy. Turn the standby or copies off first.
- Databases in use for logical replication, as publisher or subscriber.
- Databases with active scheduled jobs (
pg_cron). A sleeping database runs no scheduled jobs. You can allow sleep anyway by accepting that, withallow_scheduled_jobs. - Redis, Valkey, MySQL, MariaDB and ClickHouse databases.
- If sleep can’t be turned on, the answer says why (
409 sleep_unavailable).
Cost and capacity
- Starter: sleep is included in your plan. Branches count toward how many can be awake at once on your plan.
- Dedicated billed hourly: a sleeping database pays for storage only. With monthly or yearly billing sleep is allowed but saves nothing, and the dashboard says so.
- A production database keeps its place while it sleeps, so waking it can’t fail for lack of room. A branch gives its place up and asks for it again when it wakes.
- Backups skip a sleeping database because nothing changes. One that slept longer than its backup interval is backed up when it next wakes.
- Once a day, each sleeping production database is woken at its quietest hour for a quick check, then put back to sleep, so a slow wake is caught before you meet it. The check shows in the database’s events.
Turn sleep on or off, and wake it
- The database’s
sleepshowsenabled,after_minutes,allow_scheduled_jobsand astateofawake,sleeping,asleeporwaking. The dashboard shows Available or Sleeping. - Name a branch as
shop-db/fix-checkoutto change its sleep timer or wake it:stackshift db sleep shop-db/fix-checkout --after 30m. POST .../wake?wait=trueanswers200once the database is awake. Withoutwaitit answers202straight away.- In Terraform, add
sleep_after_minutesto your existingstackshift_databaseresource, or tostackshift_database_instancefor a Dedicated database.
Availability
Sleep when idle is rolling out. If your database’s Settings page doesn’t show it yet, it isn’t switched on for your account.Expected result
The database shows Sleeping when it’s idle, and the next connection from your app succeeds after a short wait with no change to its address or password.
Common failures
Related guides
Branches
A copy of your database you can change freely. It has its own address and password, and sleeps when nobody’s using it.
Branches for previews and agents
Give every pull request and every agent task a database it can’t hurt production with, and try migrations there first.
Database connections and tunneling
Use private, direct public, or StackShift tunnel endpoints with TLS and explicit client IP policy.