My question is not specific to PostgreSQL 9.6, but PostgreSQL 9.6 is the software I'm trying to run so I will use it as my example. I'm also running on Windows 10.

EDIT: Magoo gave a very PostgreSQL specific answer. If someone else could give a more general answer that works then I will change which is the accepted answer to that one.

I'm running the following code in a batch-file:

@echo off
SET/P file=Select file:
@echo on
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file1]
"%~dp0\osm2pgsql-bin\osm2pgsql.exe" --slim --drop --latlong --keep-coastlines --multi-geometry --hstore -S [file2] --tag-transform-script [file3] -W -U gisuser -H [IP adress] -d [databasename] "%file%"
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file4]
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file5]
"%~dp0\psql.exe" -W -h [IP adress] -d [databasename] -U [username] -f [file6]
@echo off
pause
@echo on

(Everything inside brackets is a hardcoded value.)

When psql.exe and osm2pgsql.exe start they prompt for a password. This is fine on its own, but with them running a number of times it forces the user to enter the password several times. osm2pgsql is also a process that takes a long time so the user would have to wait between times to enter the password.

What I want to do is prompt the user for the password once and then automatically enter that password while psql.exe and osm2pgsql.exe are running when they prompt for the password.

I've attempted this with no success: https://stackoverflow.com/a/43896549/7398644


For the curious, the code above is supposed to DROP CASCADE a number of tables in a PostgreSQL database (SQL code in file1), then recreate those tables from OpenStreetMap data, and then finally create a number of views in the same database (SQL code in file4, 5 and 6).

2

There are 2 answers

2
Magoo On BEST ANSWER

From the PostgreSQL page on command-line execution of psql.exe

-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.

Note that this option will remain set for the entire session, and so it affects uses of the meta-command \connect as well as the initial connection attempt.

So, I'd suggest a .pgpass file is the way to go...

0
Hans Hubert Vogts On

One possibility is to use a script language like Autoit or Autohotkey.

> AutoIt

AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying “runtimes” required!

>

You can create a script which is started with your batch and waits in background for a specific windows dialog (the password dialog) If the dialog is recognized, you are able to ask for the password, store it in a variable and fill out automatically in all subsequent dialog calls.