142

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}
else
    echo "reindex for unq_vbvdata_vehicle failed" >> ${LOG_FILE}
    exit 1
fi

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?

Majid Azimi
  • 2,261
  • 3
  • 23
  • 24

7 Answers7

195

You have four choices regarding the password prompt:

  1. set the PGPASSWORD environment variable. For details see the manual:
    http://www.postgresql.org/docs/current/static/libpq-envars.html
  2. use a .pgpass file to store the password. For details see the manual:
    http://www.postgresql.org/docs/current/static/libpq-pgpass.html
  3. use "trust authentication" for that specific user:
    http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST
  4. use a connection URI that contains everything:
    http://www.postgresql.org/docs/current/static/libpq-connect.html#AEN42532
49

A simple example with PGPASSWORD will be something like:

PGPASSWORD=YOUR_PASSRORD psql -h YOUR_PG_HOST -U YOUR_USER_NAME

Hope it helps.

Eric
  • 591
  • 4
  • 5
  • 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 Oct 19 '17 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 May 03 '19 at 14:39
  • 5
    Note, that this would still expose your password to the command shell history. – Stefan Haberl Jan 30 '20 at 07:44
  • 1
    Don't forget to restart your command prompt on Windows if you're adding this permanently to Path. – Cloud Sep 30 '22 at 07: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 Mar 28 '23 at 13:05
5

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.
PGPASSWORD=YOUR_PASSRORD psql -h YOUR_PG_HOST -U YOUR_USER_NAME    

# this is more likely to work, assuming given account has permissions to that database.
PGPASSWORD=YOUR_PASSRORD psql -h YOUR_PG_HOST -U YOUR_USER_NAME -d YOUR_DATABASE  
András Váczi
  • 31,278
  • 13
  • 101
  • 147
mtoonen
  • 51
  • 1
  • 1
5

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
Jeremy Thompson
  • 201
  • 3
  • 10
2

Way-1:

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

Way-2:

psql postgres://<db_user>:<user_password>@<hostname/domain>:<db_port>/<db_name>
Sadia
  • 21
  • 1
0

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.

Dhiraj
  • 101
  • 1
-2
 var processStartInfo = new ProcessStartInfo();
 processStartInfo.UseShellExecute = false;
 processStartInfo.EnvironmentVariables["PGPASSWORD"] = "password";
 Process proc = Process.Start(processStartInfo);

It will work.

Hannah Vernon
  • 70,041
  • 22
  • 171
  • 315
  • 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 Aug 18 '21 at 15:55
  • Ok, maybe missing what was written by Hannah but still very helpful for me :-) – user2126375 Sep 21 '21 at 13:41