1

Hi I'm trying to create a database with owner ! the owner by default is postgres and I want to change it when a user creates a database

read -p "Enter owner database name : " $owner
read -p "Enter database name : " $my_db
sudo -i -u postgres psql -c 'CREATE DATABASE $my_db OWNER $owner'

I tested this line but it create a database name 'owner '

can someone help me ??

fra-san
  • 10,205
  • 2
  • 22
  • 43

1 Answers1

1

I see two issues in your code snippet:

  1. $owner in

    read -p "Enter owner database name : " $owner
    

    expands the owner variable; it should be

    read -p "Enter owner database name : " owner
    

    without the $; same thing for $my_db;

  2. The single quotes in

    sudo -i -u postgres psql -c 'CREATE DATABASE $my_db OWNER $owner'
    

    prevent the expansion of $my_db and $owner. It should be

    sudo -i -u postgres psql -c "CREATE DATABASE $my_db OWNER $owner"
    

    (note the double quotes in place of single quotes). A thorough explanation can be found in What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?

fra-san
  • 10,205
  • 2
  • 22
  • 43