By shabu 224

2013-03-12 10:59:25 8 Comments

I am trying to execute this psql command using a batch script:

psql --host=localhost --dbname=<dbname> --port=<Port Number>
     --username=<dbuser> --file=C:\PSQL_Script.txt --output=C:\PSQL_Output.txt

The problem is that it's asking for the password every time I execute the batch script. How can I password argument through the batch file?


@Darshana Patel 2018-09-28 12:28:25

postgres Documentation -

-w --no-password Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

@Erwin Brandstetter 2013-03-23 23:01:13

Keep reading, the best options come last. But let's clarify a couple of things first.

Only silence the password request

If your issue is only the password prompt, you can silence it. I quote the manual here:


Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as a .pgpass file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password. (...)

You probably don't need a password

Normally this is unnecessary. The default database superuser postgres usually corresponds to the system user of the same name. Running psql from this account doesn't require a password if the authentication method peer or ident are set in your pg_hba.conf file. You probably have a line like this:

local    all    postgres    peer

And usually also:

local    all    all         peer

This means, every local user can log into a all database as database user of the same name without password.
However, there is a common misconception here. Quoting again:

This method is only supported on local connections.

Bold emphasis mine.
You are connecting to localhost, which is not a "local connection", even though it has the word "local" in it. It's a TCP/IP connection to Wikipedia on localhost:

On modern computer systems, localhost as a hostname translates to an IPv4 address in the (loopback) net block, usually, or ::1 in IPv6.

Simple solution for local connections

Omit the parameter -h from the psql invocation. Quoting the manual on psql once more:

If you omit the host name, psql will connect via a Unix-domain socket to a server on the local host, or via TCP/IP to localhost on machines that don't have Unix-domain sockets.


... doesn't have Unix-domain sockets, pg_hba.conf lines starting with local are not applicable on Windows. On Windows you connect via localhost by default, which brings us back to the start.

If your security requirements are lax, you could just trust all connections via localhost:

host    all    all     trust

I would only do that for debugging with remote connections off. For some more security you can use SSPI authentication on Windows. Add this line to pg_hba.conf for "local" connections:

host    all    all     sspi

If you actually need a password

You could set an environment variable, but this is discouraged, especially for Windows. The manual:

PGPASSWORD behaves the same as the password connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using the ~/.pgpass file (see Section 32.15).

The manual on psql:

A conninfo string is an alternative to specify connection parameters:

 $ psql "user=myuser password=secret_pw host=localhost port=5432 sslmode=require"

Or a URI, which is used instead of a database name:

 $ psql postgresql://myuser:[email protected]:5432/mydb?sslmode=require

Password File

But it's usually preferable to set up a .pgpass file rather than putting passwords into script files.
Read the short chapter in the manual carefully. In particular, note that here ...

A host name of localhost matches both TCP (host name localhost) and Unix domain socket (pghost empty or the default socket directory) connections coming from the local machine.

Exact path depends on the system. This file can store passwords for multiple combinations of role and port (DB cluster):


On Windows machines look for the file in:


%APPDATA% typically resolves to: C:\Documents and Settings\My_Windows_User_Name\Application Data\.

@nyxz 2015-11-19 10:12:22

I had kinda same problem:

psql -hlocalhost -d<myDB> -U<myUser>

always prompted me for password. This is as @Erwin explained because of -hlocalhost is connecting through TCP and not through the Unix-domain socket (for Unix based OS). So even if you've configured your local as trusted:

local   all    all                     trust

it will still prompt for password. So in order to configure the -hlocalhost to work through TCP I had to configure the host for localhost addresses, like so:

host    all    all     trust
host    all    all    ::1/128          trust

But this didn't work for me. What I had to do is combine both of those as:

host    all    all    localhost        trust

Some additional readings:

Related Questions

Sponsored Content

8 Answered Questions

[SOLVED] How to get a password from a shell script without echoing

  • 2010-10-20 17:33:28
  • BD at Rivenhill
  • 273863 View
  • 333 Score
  • 8 Answer
  • Tags:   bash shell scripting sh

9 Answered Questions

[SOLVED] How to run the sftp command with a password from Bash script?

7 Answered Questions

[SOLVED] How to exit from PostgreSQL command line utility: psql

6 Answered Questions

[SOLVED] psql - save results of command to a file

  • 2011-03-16 20:26:16
  • pstanton
  • 172179 View
  • 206 Score
  • 6 Answer
  • Tags:   postgresql psql

41 Answered Questions

[SOLVED] Shell command to sum integers, one per line?

  • 2009-01-16 15:42:03
  • Andrzej Doyle
  • 475948 View
  • 739 Score
  • 41 Answer
  • Tags:   shell

0 Answered Questions

Password didn't match psql

  • 2018-07-12 17:25:25
  • Mark
  • 26 View
  • 0 Score
  • 0 Answer
  • Tags:   shell psql

1 Answered Questions

[SOLVED] psql cmd command specify password (through java application)

17 Answered Questions

[SOLVED] psql: FATAL: database "<user>" does not exist

  • 2013-07-13 19:18:24
  • Ryan Rich
  • 253926 View
  • 555 Score
  • 17 Answer
  • Tags:   postgresql psql

1 Answered Questions

Using psql to run a file with both standard query language and meta-commands

  • 2017-03-07 01:00:33
  • dannygene
  • 56 View
  • 0 Score
  • 1 Answer
  • Tags:   psql

2 Answered Questions

[SOLVED] PSQL run from shell script - command not found?

  • 2013-11-25 14:16:01
  • jakkolwiek
  • 974 View
  • 0 Score
  • 2 Answer
  • Tags:   bash shell psql

Sponsored Content