Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute PostgreSQL script-file from command line without userinput / password

During the installation of my app, I want to create a PostgreSQL-Database and some tables and functions.

For that purpose I use PSQL.EXE that ships with PostgreSQL. I have 2 scripts. The first one creates the database and a corresponding user that has rights to execute scripts on that database. I want to execute the second script as this just created user. Unfortunately I can't find a way to pass the password for that user as a command line argument. Omitting the password leads to a stop of execution and a prompt for the user to enter the password, which I would like to avoid - since this is executed during installtion of my app.

Is there any way to pass the password as argument or is there any other command line tool I could use?

To explain the environment a bit further. I use WiX 3.5 setup as a "MSI-Builder".

like image 753
derSteve Avatar asked Jun 14 '12 18:06

derSteve


1 Answers

You can either use a pgpass file as dbenhur answerd, or you can set the environment variable PGPASSWORD before calling psql:

SET PGPASSWORD=my_very_secret_password
psql somedb someuser

All supported environment variables are documented in the manual: http://www.postgresql.org/docs/current/static/libpq-envars.html

like image 114
a_horse_with_no_name Avatar answered Nov 03 '22 00:11

a_horse_with_no_name