I wrote a script to REINDEX indexes in a database. Here is one of them:

echo -e "\nreindex for unq_vbvdata_vehicle started at: `date "+%F %T"`" >> ${LOG_FILE}
psql -U ${USERNAME} -h ${HOSTNAME} -d ${DBNAME} -c "REINDEX INDEX scm_main.unq_vbvdata_vehicle;"
if [[ ${?} -eq 0 ]]; then
    echo "reindex for unq_vbvdata_vehicle finished at: `date "+%F %T"`" >> ${LOG_FILE}
    echo "reindex for unq_vbvdata_vehicle failed" >> ${LOG_FILE}
    exit 1

The problem is I can not run this script in standalone mode. psql is prompting password every time it runs. There is also two limitations:

  1. I can not create a user on database with no password.

  2. Because REINDEX locks tables, I should use sleep <num> between each REINDEX.

Is there any automatic solution?


You have four choices regarding the password prompt:

  1. set the PGPASSWORD environment variable. For details see the manual:
  2. use a .pgpass file to store the password. For details see the manual:
  3. use "trust authentication" for that specific user:
  4. use a connection URI that contains everything:
| improve this answer | |
  • 4
    Since PostgreSQL 9.1 there is also the peer authentication method for local connections. Currently only for Linux, BSD, OS X or Solaris (not Windows). – Erwin Brandstetter Mar 11 '12 at 2:00
  • 1
    A note on the .pgpass option, you still have to specify username, database, and hostname (if you would've normally) in the psql command – raphael Feb 10 '17 at 18:41
  • with respect to 3, you need to edit the method. its explained here: gist.github.com/p1nox/4953113 – FuzzyAmi Apr 17 '19 at 9:33
  • Easily use export PGPASSWORD="your_pw" to go version 1 – gies0r Aug 13 '19 at 20:34
  • @gies0r I would never export a password because of security considerations – Stefan Haberl Jan 30 at 7:43

A simple example with PGPASSWORD will be something like:


Hope it helps.

| improve this answer | |
  • 4
    Hi Ian_H, thank for your comments and I all agree with your points. The accepted answer is very comprehensive and that did solve my problem. The reason I put my answer to this old post is because it is still not straightforward to me, as I believe others may have the same feeling. – pdm Oct 19 '17 at 20:23
  • This was definitely a good addition to the accepted answer - helpful reminder that we don't need to permanently set ENV vars. – kevlarr May 3 '19 at 14:39
  • 1
    Note, that this would still expose your password to the command shell history. – Stefan Haberl Jan 30 at 7:44

Depending your account permissions, the example without specifying the database may fail, because user permissions are checked against the database you connect to. It is better explicitly specify the database too.

# this can fail.

# this is more likely to work, assuming given account has permissions to that database.
| improve this answer | |

Very helpful answers in this thread. I'm just adding this for ubuntu 18.04:

sudo PGPASSWORD=yourpasswordHere -u postgres psql

This will take you into the postgres without the password prompt, without having to set any environment variables. This is not a permanent setting.

| improve this answer | |
  • Don't do this: The password will be visible in the output of ps -ef. – Colin 't Hart Oct 14 '19 at 13:09

In Windows you can set it system wide:

SETx PGPASSWORD YourPassword /M 

Or for security if you don't want it system wide you can login in one line:

set PGPASSWORD=YourPassword&& psql -d database -U user
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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