Deploying a Postgres Database
Postgres (PostgreSQL) is a powerful, open-source object-relational database system known for its reliability, feature set, and extensibility. It supports advanced data types, ACID compliance, and is widely used for applications requiring robust data integrity and performance.
This guide explains how to deploy a Postgres database to Klutch.sh using a Dockerfile, including installation, sample configuration, and persistent volume setup.
Prerequisites
- Docker installed (Download)
- Git and GitHub account
- Klutch.sh account
Getting Started: Sample Dockerfile
Create a Dockerfile
in your project root:
FROM postgres:16-alpine
# Set environment variables (can be overridden in Klutch.sh UI)ENV POSTGRES_DB=mydbENV POSTGRES_USER=myuserENV POSTGRES_PASSWORD=mypassword
# Expose default Postgres portEXPOSE 5432
# Optional: Add custom initialization scripts# COPY ./init.sql /docker-entrypoint-initdb.d/
Sample Usage
You can connect to your Postgres database from any app using the following connection string:
postgresql://myuser:mypassword@<APP_HOST>:5432/mydb
Replace <APP_HOST>
with the internal Klutch.sh service hostname or external IP as needed.
Deploying with Persistent Volumes
To ensure your database data persists across deployments and restarts, attach a persistent volume in Klutch.sh:
- Push your code (with Dockerfile) to a GitHub repository.
- Log in to Klutch.sh.
- Create a new project.
- Create a new app:
- Select your Postgres GitHub repository and branch
- Choose the Dockerfile option when prompted
- Set service details and environment variables as needed (POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD)
- Attach a persistent volume:
- In the “Volumes” section, add a new volume (e.g.,
pgdata
) - Mount path:
/var/lib/postgresql/data
- In the “Volumes” section, add a new volume (e.g.,
- Configure region, compute, and other settings as needed
- Click “Create” to deploy. Klutch.sh will build your Docker image, attach the volume, and start your Postgres database.
Resources
Deploying Postgres to Klutch.sh with a Dockerfile and persistent volumes ensures your data is safe and your database is production-ready.