保存 psql 历史文件时出错

Error saving psql history file

我目前的psql版本是10.1, 但是当我输入 \q 来保存它显示的历史时:

postgres=# \q could not save history to file "/opt/PostgreSQL/9.3/.psql_history": No such file or directory

但我正在使用 Postgres 10.1,如何解决这个问题?

psql如何确定历史文件的路径是documented如:

HISTFILE

The file name that will be used to store the history list. If unset, the file name is taken from the PSQL_HISTORY environment variable. If that is not set either, the default is ~/.psql_history, or %APPDATA%\postgresql\psql_history on Windows

你必须知道,psql 不使用 HOME 来计算 tidle 字符表示的主目录,它使用 /etc/passwd.

所以问题中推测psql是由postgres用户发起的,创建这个用户的时候是为了在本机上安装PostgreSQL 9.3,后来没有改的时候/opt/PostgreSQL/9.3/ 已被删除,因此 /etc/passwd 中的条目仍指向该不存在的目录。

This answer on DBA.se 给出 shell 命令来解决这个问题:

sudo usermod --home '/path/to/database' postgres