> For the complete documentation index, see [llms.txt](https://tuonioooo-notebook.gitbook.io/docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tuonioooo-notebook.gitbook.io/docker/advanced/n8n/withpostgres.md).

# n8n with PostgreSQL

基于 PostgreSQL 数据库的 n8n 部署配置。

## 组件说明

### init-data.sh 脚本

这个初始化脚本的主要作用是在 PostgreSQL 数据库启动时进行必要的用户设置：

1. 创建非 root 用户（由 `POSTGRES_NON_ROOT_USER` 环境变量指定）
2. 设置用户密码（由 `POSTGRES_NON_ROOT_PASSWORD` 环境变量指定）
3. 为该用户授予必要的数据库权限：
   * 数据库的所有权限
   * public schema 的 CREATE 权限

这样可以确保 n8n 使用最小权限原则访问数据库，提高系统安全性。

## 快速开始

### 环境变量配置

在启动服务之前，请先在 [`.env`](https://github.com/tuonioooo/docker/blob/master/advanced/n8n/withPostgres/.env/README.md) 文件中配置以下环境变量：

* `POSTGRES_USER`: PostgreSQL 的超级用户
* `POSTGRES_PASSWORD`: 超级用户密码
* `POSTGRES_DB`: 数据库名称
* `POSTGRES_NON_ROOT_USER`: n8n 使用的普通用户账号
* `POSTGRES_NON_ROOT_PASSWORD`: n8n 用户密码

**重要：** 请确保修改默认的用户名和密码！

### 启动服务

在当前目录执行以下命令启动服务：

```bash
docker-compose up -d
```

### 停止服务

执行以下命令停止服务：

```bash
docker-compose stop
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://tuonioooo-notebook.gitbook.io/docker/advanced/n8n/withpostgres.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
