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