PostgreSQL: 安装时提示 The database cluster initialisation failed

3,224 阅读2分钟

本文正在参加「金石计划」


PostgreSQL: 安装时提示 The database cluster initialisation failed

problem running post-install step
installation may not complete correctly
The database cluster initialisation failed.

碰到该问题是因为没有使用默认 Locale ,选择的Locale 系统不支持。

解决方法

出现该问题,等安装程序执行完成后,使用 initdb 手动初始化数据库。
初始化数据库时,一定要指定用户( -U postgres),

命令是:

$ initdb -U postgres -d ./data -E UTF8 --locale=ja_JP

-d 后面指定的是数据放置的目录。

-E 是编码,--locale 是区域语言。

这里的 -E UTF8 --locale=ja_JP 是日语的 UTF8 ,可以不指定使用默认。

执行结果如下:

$ initdb -U postgres -d ./data -E UTF8 --locale=ja_JP

Running in debug mode.

The files belonging to this database system will be owned by user "bettersun".

This user must also own the server process.

...

Data page checksums are disabled.

creating directory data ... initdb: error: could not create directory "data": Permission denied

碰到该错误,是由于 data 所在目录的权限不足,需要修改一下 data 所在目录的权限。
该问题是在 Mac 上碰到的。

例如 data 的位置是 develop/data
那可以进入到 develop 所在目录(develop 的父目录),然后执行如下命令修改文件权限

chmod 777 ./develop/

然后再次运行 initdb ,控制台会输出一堆信息后,最后提示成功。

最后的控制台信息如下:

...

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections

initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:
    pg_ctl -D ./data -l logfile start

再按照提示启动数据库服务:

$ pg_ctl -D ./data -l logfile start

waiting for server to start.... done

server started

注意:不能关闭控制台,否则数据库服务就停止了。

initdb 后面不指定用户时,连接时会提示如下的错误: iShot2023-03-10 11.32.40.png

could not initiate GSSAPI security context: The operation or option is not available:Credential for asked mech-type mech not found in the credential handle connection to server at "127.0.0.1", port 5432 failed: FATAL: role "postgres" does not exist

后记

initdb 在Windows 上有个很奇怪的BUG。

initdb -U postgres -d ./data -E UTF8 --locale=xx_xx

上面命令中的 locale 不是真实存在的。
但是在 Windows 上运行命令居然能通过,在 Mac 上会提示 locale 不存在。


本文正在参加「金石计划」