|
| 1 | +# n8n |
| 2 | + |
| 3 | +[](ASK RAPH) |
| 4 | + |
| 5 | +This sample shows how to get a [n8n](https://n8n.io/) app up using PostgreSQL as a database and running with Defang. The original sample can be found [here](https://github.com/n8n-io/n8n-hosting/tree/main/docker-compose/withPostgres). The official n8n guide for setting up with PostgreSQL [here](https://docs.n8n.io/hosting/installation/server-setups/docker-compose/) does not work with Defang. To use the version compatible with Defang, please refer to this sample instead. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +1. Download [Defang CLI](https://github.com/DefangLabs/defang) |
| 10 | +2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account |
| 11 | +3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/) |
| 12 | + |
| 13 | +## Development |
| 14 | + |
| 15 | +**IMPORTANT:** But before you do that change the default users and passwords in the [`.env`](.env) file! |
| 16 | + |
| 17 | +To run the application locally for development, use the development compose file: |
| 18 | + |
| 19 | +```bash |
| 20 | +docker compose -f compose.dev.yaml up |
| 21 | +``` |
| 22 | + |
| 23 | +This will: |
| 24 | + |
| 25 | +- Start PostgreSQL with volume persistence for local development |
| 26 | +- Expose PostgreSQL on port 5432 for direct access if needed |
| 27 | +- Start n8n on port 5678 with volume persistence |
| 28 | +- Use the `init-data.sh` script to automatically set up the non-root database user |
| 29 | + |
| 30 | +You can access n8n at `http://localhost:5678` once the containers are running. |
| 31 | + |
| 32 | +## Configuration |
| 33 | + |
| 34 | +For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/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. |
| 35 | + |
| 36 | +### `POSTGRES_USER` |
| 37 | + |
| 38 | +The username for your Postgres database. You need to set this before deploying for the first time. |
| 39 | + |
| 40 | +### `POSTGRES_PASSWORD` |
| 41 | + |
| 42 | +The password for your Postgres database. You need to set this before deploying for the first time. |
| 43 | + |
| 44 | +### `POSTGRES_DB` |
| 45 | + |
| 46 | +The name of your Postgres database. You need to set this before deploying for the first time. |
| 47 | + |
| 48 | +### `POSTGRES_NON_ROOT_USER` |
| 49 | + |
| 50 | +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. |
| 51 | + |
| 52 | +### `POSTGRES_NON_ROOT_PASSWORD` |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +## Deployment |
| 57 | + |
| 58 | +The project includes two compose files: |
| 59 | + |
| 60 | +- **`compose.dev.yaml`** - For local development with volume persistence and port exposure |
| 61 | +- **`compose.yaml`** - For cloud deployment (extends `compose.dev.yaml` with cloud-specific optimizations) |
| 62 | + |
| 63 | +> [!NOTE] |
| 64 | +> Download [Defang CLI](https://github.com/DefangLabs/defang) |
| 65 | +
|
| 66 | +### Defang Playground |
| 67 | + |
| 68 | +Deploy your application to the Defang Playground by opening up your terminal and typing: |
| 69 | + |
| 70 | +```bash |
| 71 | +defang compose up |
| 72 | +``` |
| 73 | + |
| 74 | +This will use `compose.yaml` which extends `compose.dev.yaml` but: |
| 75 | + |
| 76 | +- Removes volume mounts (not supported by Defang) |
| 77 | +- Removes PostgreSQL port exposure for security |
| 78 | +- Adds a `release` service to initialize the database with proper user permissions |
| 79 | +- Optimizes environment variables for cloud deployment |
| 80 | + |
| 81 | +### BYOC (AWS) |
| 82 | + |
| 83 | +If you want to deploy to your own cloud account, you can use Defang BYOC: |
| 84 | + |
| 85 | +1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`. |
| 86 | +2. Run in a terminal that has access to your AWS environment variables: |
| 87 | + ```bash |
| 88 | + defang --provider=aws compose up |
| 89 | + ``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +Title: n8n |
| 94 | + |
| 95 | +Short Description: A n8n app running on Defang. |
| 96 | + |
| 97 | +Tags: n8n, PostgreSQL, Docker |
| 98 | + |
| 99 | +Languages: Docker, Shell |
0 commit comments