psql错误:无法连接到数据库template1:致命错误:角色"root"不存在。

11 浏览
0 Comments

psql错误:无法连接到数据库template1:致命错误:角色"root"不存在。

我在Ubuntu 20.04上安装了PostgreSQL,然后需要在脚本中执行一系列操作:\n

#!/bin/bash
main()
{
        local randpass=$(date +%s | sha256sum | base64 | head -c 32)
        #添加PostGres密码文件以修改数据库
        newlines=$(printf ":5432::$USER:$randpass\nlocalhost:5432:*:$USER:$randpass\n127.0.0.1:5432:*:$USER:$randpass\n" )
        echo -e "$newlines$(cat $HOME/.pgpass >/dev/null)" > $HOME/.pgpass
        chmod og-rw $HOME/.pgpass
        #创建数据库用户和表
        echo "CREATE ROLE \"$USER\" WITH CREATEDB LOGIN NOSUPERUSER NOCREATEROLE PASSWORD '$randpass'" |  sudo -u postgres psql
        echo "ALTER ROLE \"$USER\" WITH PASSWORD '$randpass'" |  sudo -u postgres psql
        dropdb peasoup_$USER
        createdb peasoup_$USER
        #设置数据库以存储PEASOUP信息
        source set_env_vars
        $PEASOUP_HOME/tools/db/pdb_setup.sh
}
main 

\n我将USER设置为test,但在运行脚本后,我收到了一个错误:\n

root@xxx:/xxx# ./xxxx_setup.sh 
CREATE ROLE
ALTER ROLE
dropdb: error: could not connect to database template1: FATAL:  role "root" does not exist
createdb: error: could not connect to database template1: FATAL:  role "root" does not exist
psql: error: FATAL:  database "peasoup_test" does not exist
psql: error: FATAL:  database "peasoup_test" does not exist

\n但是脚本中有一个切换用户的操作sudo -u postgres psql,为什么报错的用户仍然是root呢?

0