I have a single batch file that runs various psql and ogr2ogr (spatial/GIS) commands within PostgreSQL databases. I run identical commands across two identical databases (dev & test). I'd like to store my PostreSQL connection info in a single variable and call it to run my psql commands. That way I can simply comment out the connection (dev or test) which I'm not currently using.
The reason I'm using this workflow is because I also run ogr2ogr commands in the same script, which successfully uses a simply batch variable.
Scenario 1: Manually update connections. This works but is tedious to update
::Run commands in DEV Server/DB
psql U UserName -h 99.99.999.999 -d MyDB -c "some sql command1"
ogr2ogr command 1
psql U UserName -h 99.99.999.999 -d MyDB -c "some sql command2"
psql U UserName -h 99.99.999.999 -d MyDB -c "some sql command3"
::Run commands in TEST Server/DB
psql U UserName -h 88.88.888.888 -d MyDB -c "some sql command1"
ogr2ogr command 1
psql U UserName -h 88.88.888.888 -d MyDB -c "some sql command2"
psql U UserName -h 88.88.888.888 -d MyDB -c "some sql command3"
Scenarios 2: I would like to set DB connections in 1 one place. Then I can run all commands and switch between DBs by simply commenting out 1 line and re-running the batch.
\SET dbConnect "-U UserName -h 99.99.999.999 -d MyDevDB"
::\SET dbConnect "-U UserName -h 88.88.888.888 -d MyTestDB"
psql dbConnect -c "some sql command1"
ogr2ogr command 1
psql dbConnect -c "some sql command2"
psql dbConnect -c "some sql command3"
I've tried several options include the -v, set, and /set to no avail. I've researched these cases below, but am still struggling. Ideally I'd like to store and call the variables in one location, in one file.
psql passed variable How do I specify a password to psql non-interactively? PostgreSQL Connection URL