I have installed postgresql on ubuntu20.04, then I need to perform a series of actions in the script:
#!/bin/bash
main()
{
local randpass=$(date +%s | sha256sum | base64 | head -c 32)
#Add PostGres password file for modifying the database
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
#Create Database User and Table
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
#Setup the Database to store PEASOUP info
source set_env_vars
$PEASOUP_HOME/tools/db/pdb_setup.sh
}
main
I set USER to test, but after running the script, I get an error:
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
But there's a switch in the script, sudo -u postgres psql
Why is the user reporting the error still root?