I am writing a non-interactive script that needs to execute postgres commands. The script will only be run by root or a user with sudo permissions, and the postgres commands need to be executed as the postgres user in postgresql. The trouble is that postgresql always (as far as I can tell) prompts for a password interactively (i.e. pqsl -U postgres -W) or does not accept a password at all (i.e. psql -U postgres -w). I would like to pass a password on the command line, similar to how mysql allows mysql -u user -p password. How can I do this with postgres?

share|improve this question
1  
What language are you using? Shell? – Faheem Mitha Apr 22 '14 at 17:56
    
You shouldn't do that. -p password leaves your password visible in, for example, ps xa output. – derobert Apr 22 '14 at 17:59
    
I'm using a bash script. And @derobert, so if I shouldn't do it this way, then how do I run postgres commands non-interactively at all? I'm going to need to authenticate to the postgres server somehow. – jayhendren Apr 22 '14 at 18:02
    
@jayhendren you can set up your pg_hba.conf file to use peer or ident authentication (if the script is running on the same machine as postgres) or you can use a ~/.pgpass file. – derobert Apr 22 '14 at 18:05
    
It will ask for passwords even if you execute your script as postgres using crontab? I have some scripts running directly as postgres scheduled, and it works out of the box... – nwildner May 31 '16 at 14:02
up vote 2 down vote accepted

There are two ways of doing the same thing .

Approach 1 :

Add a .pgpass file in home directory of user(user which runs the script)

cat /home/user/.pgpass hostname:port:database:user:password

chmod 600 /home/user/.pgpass

After adding the above you don't need to add password in any script you using. It will connect directly.

Approach 2 :

Add the following in your shell script. But this will expose your password and become a problem if you are committing to any repository.

export PGPASSWORD=password

share|improve this answer

Use the PGPASSWORD environment variable. For instance:

PGPASSWORD=<password> psql -U postgres -c "<postgresql query>"

Source

share|improve this answer
1  
Not the mosy secure solution, as the process environment can be sniffed. See .pgpass solution or modify postgres_hba.conf – Otheus May 31 '16 at 14:05

If you can run psql as the user postgres, the most useful solution is to modify pg_hba.conf so the the Unix user postgres, connecting via localhost/socket, is authenticated without any password pr external file. At the tip of the file, insert this:

local all postgres  peer

This, TBMI is the default. Perhaps someone thought they were being clever or more secure when they removed this line, idk. You could also use something like this for a monitroing service:

local zabbix zabbix peer

for which you create a username&database named "zabbix".

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.