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?

7 Answers 7


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:
  • 7
    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). Commented Mar 11, 2012 at 2:00
  • 2
    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
    Commented Feb 10, 2017 at 18:41
  • with respect to 3, you need to edit the method. its explained here: gist.github.com/p1nox/4953113
    – FuzzyAmi
    Commented Apr 17, 2019 at 9:33
  • 2
    Easily use export PGPASSWORD="your_pw" to go version 1
    – gies0r
    Commented Aug 13, 2019 at 20:34
  • 1
    @gies0r I would never export a password because of security considerations Commented Jan 30, 2020 at 7:43

A simple example with PGPASSWORD will be something like:


Hope it helps.

  • 5
    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.
    – Eric
    Commented Oct 19, 2017 at 20:23
  • 3
    This was definitely a good addition to the accepted answer - helpful reminder that we don't need to permanently set ENV vars.
    – kevlarr
    Commented May 3, 2019 at 14:39
  • 5
    Note, that this would still expose your password to the command shell history. Commented Jan 30, 2020 at 7:44
  • 1
    Don't forget to restart your command prompt on Windows if you're adding this permanently to Path.
    – Cloud
    Commented Sep 30, 2022 at 7:03
  • @StefanHaberl raises a good point. Using read can help (e.g., read -s -p "Enter password: " PGPASSWORD; export PGPASSWORD; see more at this answer).
    – toraritte
    Commented Mar 28, 2023 at 13:05

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.

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


PGPASSWORD="<password>" psql -U <db_user> -d <db_name> -h <host_name/domain> -p <port>


psql postgres://<db_user>:<user_password>@<hostname/domain>:<db_port>/<db_name>

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.

  • 2
    Don't do this: The password will be visible in the output of ps -ef. Commented Oct 14, 2019 at 13:09
 var processStartInfo = new ProcessStartInfo();
 processStartInfo.UseShellExecute = false;
 processStartInfo.EnvironmentVariables["PGPASSWORD"] = "password";
 Process proc = Process.Start(processStartInfo);

It will work.

  • I pointer or two about how to actually make this work would be helpful. For instance, you'd need Visual Studio 2019, and would need to create a solution, with a C# console project, etc, etc.
    – Hannah Vernon
    Commented Aug 18, 2021 at 15:55
  • Ok, maybe missing what was written by Hannah but still very helpful for me :-) Commented Sep 21, 2021 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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