Following the tutorial here: http://www.unixmen.com/install-postgresql-9-4-phppgadmin-ubuntu-14-10/
I am scripting the installation of PostgreSQL in bash but having a problem when it comes to setting the initial user password.
Following the tutorial for this part we have:
# Login to postgresql prompt:
sudo -u postgres psql postgres
# .. and set postgres password with following command:
postgres=# \password postgres
Enter new password:
Enter it again:
postgres=# \q
Which is great but I want to set this password from the Bash script and one way that this type of task could generally with would using Expect:
# Build the command into a variable so that we can run it later
PSQL_QUERY=$(expect -c "
set timeout 10
spawn -u postgres psql postgres -c '\\\password postgres'
expect \"Enter new password:\"
send \"password\r\"
expect \"Enter it again:\"
send \"password\r\"
expect eof
");
# Run the command with automated entry
echo "$PSQL_QUERY";
But when I try to run the script I get output as follows:
root@node:/home/user# ./test.sh
send: spawn id exp6 not open
while executing
"send "password\r""
spawn sudo -u postgres psql postgres -c '\password postgres'
psql: FATAL: Peer authentication failed for user "postgres'"
Which I don't really understand both because I am new to PostgreSQL and because when I run the following:
root@node:/home/user# sudo -u postgres psql postgres -c '\password postgres'
Enter new password: <password>
Enter it again: <password>
It works. Could someone please help me with the understanding of where I am going wrong with this and how I could get it right so that I can set the new "postgres" user password from within a bash script. Thanks in advance!