运行 来自 shell 脚本的 psql 查询

Run a psql query from a shell script

我有一个 PSQL 语句,它需要来自 shell 脚本的 运行。 我试过下面但没有成功

ctfdb 是我的数据库

psql ctfdb <<EOF
statement
EOF

psql: FATAL: Peer authentication failed for user "root"

我什至试过给 "sudo psql"

Sorry, user root is not allowed to execute '/bin/psql ctfdb' as root on my server

有什么建议吗?

psql -U postgres ctfdb

以用户 postgres 身份连接。您正在以 shell 用户名 root.

连接到数据库

还有其他方法,例如设置PGUSER环境变量或配置pg_hba.conf以允许其他用户访问。