I am trying to automate an app deployment process which includes creating a new postgresql database and configure initial settings in a few lines. I can already do this via the terminal manually, however I am struggling to get this working from an .sh script, what I have so far which is not working:
#!/bin/sh
sudo -u postgres psql
read -p "Enter a unique Postgresql db name: " DBN
CREATE DATABASE $DBN;
read -p "Enter a name for Database $DBN: " USN
read -p "Enter a password for user $USN: " USPSD
CREATE USER USN WITH PASSWORD 'USPSD';
ALTER ROLE USN SET client_encoding TO 'utf8';
ALTER ROLE USN SET default_transaction_isolation TO 'read committed';
ALTER ROLE USN SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE DBN TO USN;
\q
The above starts starts an interactive postgresql session but doesn't go any further. This is the output:
could not change directory to "/home/ubuntu": Permission denied
psql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))
Type "help" for help.
postgres=#