Quickstart
Create a new app
Go to the bunny.net dashboard, select Magic Containers, and click Add App. Select Single region deployment.
Add the PostgreSQL container
Click Add Container and configure the image:
- Registry: Docker Hub
- Image:
library/postgres - Tag:
17-alpine
POSTGRES_USER=postgresPOSTGRES_PASSWORD= a strong passwordPOSTGRES_DB=appPGDATA=/var/lib/postgresql/data/pgdata
Add a persistent volume
In the Volumes section of the container settings, add a volume:
- Name:
postgres-data - Mount path:
/var/lib/postgresql/data
Always set a strong
POSTGRES_PASSWORD, even if the database is not exposed
externally. Other containers in the same pod can access the network, and a
password protects against accidental or unauthorized access.Environment variables
The official PostgreSQL image supports these environment variables:| Variable | Description | Default |
|---|---|---|
POSTGRES_USER | Superuser name | postgres |
POSTGRES_PASSWORD | Superuser password (required) | - |
POSTGRES_DB | Default database created on first start | postgres |
PGDATA | Data directory inside the container | /var/lib/postgresql/data |
Connect from your app
In a multi-container setup, your app and PostgreSQL share the same localhost network. Connect using127.0.0.1 and the default port 5432.
- Connection string
- Node.js
- Go
- Python
- PHP
Multi-container example
A typical setup pairs PostgreSQL with your application. When configuring the app, add two containers:App container
- Image: your app image (e.g.
ghcr.io/<your-username>/my-app:latest) - Endpoint: the port your app listens on
- Environment variables:
DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@127.0.0.1:5432/app
PostgreSQL container
- Image:
postgres:17-alpine - Volume: mount path
/var/lib/postgresql/data - Environment variables:
POSTGRES_USER=postgresPOSTGRES_PASSWORD= a strong passwordPOSTGRES_DB=appPGDATA=/var/lib/postgresql/data/pgdata
Both containers share the same localhost network, so your app connects to
PostgreSQL at
127.0.0.1:5432. See multi-container
apps for more details.External access
To connect to PostgreSQL from outside Magic Containers (e.g. from your local terminal), add an Anycast endpoint:- Go to your app’s Endpoints tab and click Add New Endpoint
- Select Anycast as the type
- Set Container Port to
5432 - Set Exposed Port to
5432 - Click Add Endpoint
The exposed port and container port may differ. When connecting externally,
always use the exposed port shown in your endpoint configuration.