This sample shows how to get a n8n app up using PostgreSQL as a database and running with Defang. The original sample can be found here. The official n8n guide for setting up with PostgreSQL here does not work with Defang. To use the version compatible with Defang, please refer to this sample instead.
- Download Defang CLI
- (Optional) If you are using Defang BYOC authenticate with your cloud provider account
- (Optional for local development) Docker CLI
IMPORTANT: But before you do that change the default users and passwords in the .env
file!
To run the application locally for development, use the development compose file:
docker compose -f compose.dev.yaml up
This will:
- Start PostgreSQL with volume persistence for local development
- Expose PostgreSQL on port 5432 for direct access if needed
- Start n8n on port 5678 with volume persistence
- Use the
init-data.sh
script to automatically set up the non-root database user
You can access n8n at http://localhost:5678
once the containers are running.
For this sample, you will need to provide the following configuration. Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
The username for your Postgres database. You need to set this before deploying for the first time.
The password for your Postgres database. You need to set this before deploying for the first time.
The name of your Postgres database. You need to set this before deploying for the first time.
The non-root user for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called init-data.sh
. You need to set this before deploying for the first time.
The POSTGRES_NON_ROOT_USER's password for your Postgres database, it will be used in the service release to setup non-root access for your Postgres database. It is also used in the script called init-data.sh
. You need to set this before deploying for the first time.
The project includes two compose files:
compose.dev.yaml
- For local development with volume persistence and port exposurecompose.yaml
- For cloud deployment (extendscompose.dev.yaml
with cloud-specific optimizations)
Note
Download Defang CLI
Deploy your application to the Defang Playground by opening up your terminal and typing:
defang compose up
This will use compose.yaml
which extends compose.dev.yaml
but:
- Removes volume mounts (not supported by Defang)
- Removes PostgreSQL port exposure for security
- Adds a
release
service to initialize the database with proper user permissions - Optimizes environment variables for cloud deployment
If you want to deploy to your own cloud account, you can use Defang BYOC:
- Authenticate your AWS account, and check that you have properly set your environment variables like
AWS_PROFILE
,AWS_REGION
,AWS_ACCESS_KEY_ID
, andAWS_SECRET_ACCESS_KEY
. - Run in a terminal that has access to your AWS environment variables:
defang --provider=aws compose up
Title: n8n
Short Description: A n8n app running on Defang.
Tags: n8n, PostgreSQL, Docker
Languages: Docker, Shell