0

I want to set all this syntax in variable

 su -l postgres -c "psql -c \"CREATE DATABASE graphite WITH OWNER graphite\""

 CREATE DATABASE 

so I wrote this

 res=$(  su -l postgres -c "psql -c \"CREATE DATABASE graphite WITH OWNER graphite\"" )

CREATE DATABASE 

but $res is empty

echo $res

I also tried to add " " but without success.

How to insert the results of

 su -l postgres -c "psql -c \"CREATE DATABASE graphite WITH OWNER graphite\""

to a shell variable?

yivi
  • 163
  • 1
  • 2
  • 11
jango
  • 59
  • 2
  • 3
  • 12

1 Answers1

0

The question is a little hard to follow.

By "insert the results of....." are you inserting the database name 'graphite' as a variable for other commands in the script?

How are you handling the password prompt that should come up when you switch users? Or is this script being run by root?

And I'm confused why you are using the -l option -- that's typically to list commands. Do you want this command run as postgres? Then you should use the -u postgres option, telling the command to run this as postgres user.

Thirdly, make sure you have a way to enter credentials/password for the su user, depending on what user is actually running the script.

Assuming that, you could:

  1. Run the whole script as sudo, rather than trying to change users inside the script, which typically generates a password prompt.

  2. Have you tried using sudo -u postgres? The postgres user has to be in the /etc/sudoers file.

  3. On that note, what do you see in the syslog? There is likely an error there from running this script and the command not completing. And have you verified permissions to libraries, postgres commands and directories?

  4. Maybe try it as sudo -u postgres -H -- plsql "CREATE DATABASE graphite WITH OWNER graphite"

Here's a good description and examples of these commands: Linux SU, SUDO

Mika Wolf
  • 169
  • 3