Setting PGPASSWORD environment variable for Postgr

2019-05-13 17:17发布

I needs to create a tablespace in Postgres with Inno Setup.

I had run this on the command line:

SET PGPASSWORD=P0stgres
"C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h localhost -p 5432 -U postgres -d postgres -c "CREATE TABLESPACE TABLETEST OWNER postgres LOCATION E'{app}\\PATHTEST\\Db'"

I tried this in Inno Setup, but it did not work:

[Run]
Filename: {sys}\cmd.exe; Parameters: "SET PGPASSWORD=P0stgres"
Filename: {sys}\cmd.exe; Parameters: ""C:\Program Files\PostgreSQL\9.4\bin\psql.exe" -h localhost -p 5432 -U postgres -d postgres -c "CREATE TABLESPACE TABLETEST OWNER postgres LOCATION E'{app}\\PATHTEST\\Db'""

Regards

3条回答
家丑人穷心不美
2楼-- · 2019-05-13 17:44

I am able to do it. No space should be there between password and &. PGPASSWORD=password&

Filename: "cmd.exe"; Parameters: "/c set PGPASSWORD=my_db_user& ""psql"" -U my_db_user -d myappdb -a -q -f C:\my\Manager\SQL\Sequences.sql & pause"; Flags: runascurrentuser;

查看更多
Emotional °昔
3楼-- · 2019-05-13 17:48

I was able to do it work. Was a little mistake. The correct is:

[Run]
Filename: {cmd}; Parameters: "/K SET PGPASSWORD=P0stgres&""C:\Program Files\PostgreSQL\9.4\bin\psql.exe"" -h localhost -p 5432 -U postgres -d postgres -c ""CREATE TABLESPACE TABLETEST OWNER postgres LOCATION '{app}\PATHTEST\Db'""
查看更多
Explosion°爆炸
4楼-- · 2019-05-13 17:48

You have three problems:

  • The cmd.exe needs /C switch before the command.
  • The environment variable is not magically exported to the second cmd.exe instance that runs the psql.exe. You have to execute both commands within the same cmd.exe instance. One of the ways is using & "operator" (another way is using a wrapper batch file to execute both commands).
  • The double-quotes (particularly those around the C:\...\psql.exe and CREATE TABLESPACE ...) have to be doubled.
  • Not a problem per se, but you should better use {cmd} constant instead of {sys}\cmd.exe.
[Run]
Filename: "{cmd}"; Parameters: "/C SET PGPASSWORD=P0stgres& ""C:\Program Files\PostgreSQL\9.4\bin\psql.exe"" -h localhost -p 5432 -U postgres -d postgres -c ""CREATE TABLESPACE TABLETEST OWNER postgres LOCATION E'{app}\\PATHTEST\\Db'"""
查看更多
登录 后发表回答