skip to Main Content

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';"

2

Answers


  1. Chosen as BEST ANSWER
    #!/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


  2. echo "Starting Database"
    sudo service postgresql start

    After this step.(just switch to postgres)

    su – postgres

    psql -c "CREATE USER admin WITH PASSWORD ‘admin’;"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search