在引导期间创建 postgres 用户

Create postgres user during boot

我是 运行 引导期间来自 monit 的 bash 文件,该 bash 文件启动我的 postgres 服务器。

如果我的数据库目录不存在,我会:

1- initdb (postgresql/data/) su - edge -c '/usr/bin/initdb -D ${DBDIR}'

2- 将修改后的 pg_hba.conf 和 postgresql.conf 文件复制到 (postgresql/data/)

3- 启动我的服务器 su - edge -c " /usr/bin/pg_ctl -w -D ${DBDIR} -l 日志文件开始"

4-postgres createuser - su - $User -c '${DBDIR} -e -s postgres'

执行完bash文件后 postgresql/data/ 已创建 文件被复制 服务器启动, 但是没有创建用户,所以我无法访问我的数据库

错误:/usr/bin/psql -U postgres psql:致命:角色 "postgres" 不存在

我无法破译你的第 4 步,但 postgres 角色不存在的原因是因为第 1 步是 运行 用户 edge 它不要求通过-U创建postgres角色,所以它创建了一个egde角色作为超级用户。

根据 initdb 文档:

-U username
--username=username

Selects the user name of the database superuser. This defaults to the name of the effective user running initdb. It is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating system user's name is different.

或者 initdb -U postgres,或者如果您更喜欢名为 edge 的超级用户,请保持这样,但从 psql 开始 psql -U edge,或者设置 PGUSER 环境变量到 edge 以避免每次输入。