0

I have following code in my bash script to install golang and psql server, I wanted to create a user with username "admin" and password "admin". After running the script, it will entered into psql server but psql -c "CREATE USER admin WITH PASSWORD 'admin';" doesn't get execute.

enter image description here

#!/bin/bash

# Install golang
echo "Installing required package"
sudo apt-get update
sudo apt install snapd
snap install go --classic

# Install postgreSQL
echo "Installing postgreSQL"
sudo apt-get update
sudo apt install postgresql postgresql-contrib postgresql-client

# Starting database
echo "Starting Database"
sudo service postgresql start
sudo -u postgres psql

# Creating user "admin" with password "admin"
echo "Creating user 'admin' with password 'admin'"
psql -c "CREATE USER admin WITH PASSWORD 'admin';"
cyw
  • 63
  • 6
  • 2
    Because you run `sudo -u postgres psql` which starts a postgres REPL because you're running `psql`. You probably want `sudo -u postgres` only – Andy Ray Jan 03 '23 at 04:10
  • I removed `psql` and this error showed up `psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist` – cyw Jan 03 '23 at 04:26
  • Good! Get in the habit of reading errors, don't ignore them. – Andy Ray Jan 03 '23 at 04:54
  • there is also error of `sudo usage`, `sudo -u postgres` doesn't connect to the database – cyw Jan 03 '23 at 06:29

2 Answers2

0
#!/bin/bash

# Install golang
echo "Installing required package"
sudo apt-get update
sudo apt install snapd
snap install go --classic

# Install postgreSQL
echo "Installing postgreSQL"
sudo apt-get update
sudo apt install postgresql postgresql-contrib postgresql-client

# Starting database
echo "Starting Database"
sudo service postgresql start

# Creating user "admin" with password "admin"
sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'admin';"

I fixed the problem by removing sudo -u postgres psql and using sudo -u postgres psql -c "CREATE USER admin WITH PASSWORD 'admin';" Now it's working good

cyw
  • 63
  • 6
-1

echo "Starting Database" sudo service postgresql start

After this step.(just switch to postgres)

su - postgres

psql -c "CREATE USER admin WITH PASSWORD 'admin';"

  • This connected to `postgres@ip-172-31-88-173:~$ ` but still not executing `psql -c "CREATE USER admin WITH PASSWORD 'admin';"` – cyw Jan 03 '23 at 06:26