How to set up full-stack preview environments with DBLab, Coolify and GitHub
This how-to guide walks you through setting up automated preview environments that create isolated Postgres database clones for each pull request. Each environment runs independently with its own database, allowing safe testing of migrations and data changes.
What you'll achieve
By the end of this guide, you'll have:
- Automatic preview environments for every pull request
- Isolated Postgres database clones using DBLab
- Automatic deployment via Coolify
- Automatic cleanup when pull requests are closed
Prerequisites
Before starting, ensure you have:
- DBLab 4.0+ - for database cloning and branching (installation guide)
- Coolify latest version - self-hosted deployment platform (installation guide)
- GitHub repository - for code storage and CI/CD
- Virtual machine with Docker - to run DBLab and Coolify
- Postgres database - as the source for cloning
- Admin access - to both GitHub repository and Coolify instance
Step 1: Create a Coolify application
1.1 Create a new project
Open the Coolify projects page and click the + Add button.

Fill in the application name and description, and click the Create button.

1.2 Add an application resource
In the environment page, click the + Add New Resource button.

Choose your repository type:

1.3 Set up GitHub integration
For public repositories
Public Repository: Select "Public repository" and enter your repository URL directly (e.g., https://github.com/username/repo). Skip to step 2.1.
For private repositories
Private Repository: Click + Add GitHub App to create a new GitHub application.

Enter the GitHub App name and organization, then click Continue.

Configure the webhook settings:
- Select the webhook endpoint
- Important: Check the
Preview Deploymentscheckbox - Click
Register Now

GitHub will open asking you to confirm application creation. Click to confirm.

Back in Coolify, click Install Repositories On GitHub (you can find it in the Sources section in left-hand menu).

Select your target repository and click Install.

Return to the applications page and repeat steps 1.2 (Add Application Resource). Now select your GitHub Application and load the repository.

1.4 Configure application settings
Configure your application settings and click deploy:

Step 2: Configure DBLab
Before setting up preview environments, you need to configure DBLab with a data source.
2.1 Set up a data source
- Follow the DBLab configuration guide to set up your data source
- Ensure your Postgres database is accessible from the DBLab instance
- Configure the data source in your DBLab server configuration file or in the UI (recommended)
Step 3: Configure environment variables
3.1 Set up DBLab variables
In your GitHub repository, go to Settings → Secrets and variables → Actions and add these secrets:
| Secret Name | Description | Example Value |
|---|---|---|
DBLAB_API | DBLab API endpoint | https://dblab.example.com/api |
DBLAB_TOKEN | DBLab verification token | (from dblab-server config) |
DBLAB_DB_USERNAME | DBLab clone database username | postgres |
DBLAB_DB_PASSWORD | DBLab clone database password | postgres_pass |
DBLAB_DB_NAME | DBLab clone database name | postgres |
DBLAB_HOST | DBLab server IP or domain, used to connect to the database | dblab.example.com |
3.2 Set up Coolify variables
| Secret Name | Description | Example Value |
|---|---|---|
COOLIFY_URL | Coolify server URL | https://coolify.example.com |
COOLIFY_TOKEN | Coolify API token with deploy and write permissions | (generate in Keys & tokens) |
COOLIFY_APP_UUID | Your application UUID | (from app URL path) |
⚠️ Note: When generating the COOLIFY_TOKEN in Coolify's "Keys & tokens" section, the token may appear in a format like 3|token123. In this case, copy only the token part after the | separator (e.g., just token123).