Reputation: 2898
I'm running PostgreSQL 9.2 on Windows 7. I'm trying to run an Ant target using an apply tag with the executable property set to psql from a NetBeans 8.0.2 build file. I have created a pgpass.conf file in the right place with the correct password for the postgres user and I can run psql at a command prompt without having to enter a password. But when I run the Ant target, I get an error message in the output file saying the target failed because no password was supplied. What am I doing wrong? The target looks like this:
<filelist id="create-dbfiles" dir="${root}" files="createdb.sql"/>
<target name="create-db">
<apply executable="psql" addsourcefile="false" output="output.txt">
<arg value="-U postgres" />
<arg value="-w" />
<filelist refid="create-dbfiles"/>
<redirector>
<inputmapper type="glob" from="*" to="${root}\*" />
</redirector>
</apply>
</target>
Upvotes: 0
Views: 488
Reputation: 2898
a_horse_with_no_name got me thinking about the exec tag but trying to run psql
directly via an exec
tag or an apply
tag still threw up errors about a password not being supplied. Then I realised that psql
ran smoothly from the cmd prompt. I started playing around with calling cmd.exe
and passing a command to the command line, and found a solution:
<filelist id="clean-dbfiles" dir="${root}" files="cleandb.sql"/>
<target name="clean-db">
<apply executable="cmd.exe" addsourcefile="true" >
<filelist refid="clean-dbfiles"/>
<env key="PGPASSWORD" value="rootpassword"></env>
<arg value="/c"></arg>
<arg value="psql -w -U postgres -f " />
<srcfile />
</apply>
</target>
<filelist id="create-dbfiles" dir="${root}" files="createdb.sql"/>
<target name="create-db">
<apply executable="cmd.exe" addsourcefile="true" >
<filelist refid="create-dbfiles"/>
<env key="PGPASSWORD" value="rootpassword"></env>
<arg value="/c"></arg>
<arg value="psql -w -U postgres -f "/>
<srcfile />
</apply>
</target>
Here are the two key targets for creating a build script for a postgres database. Note the following points:
cmd.exe
not psql
directlyaddsourcefile
to true
because I want to call neat little self-contained SQL files for each target.apply
with a filelist
because for my createtables
or populate
targets I may have a directory of separate SQL filesVoilà! What a fuss! I had no similar difficulty with MySql because you can pass the password to the command line directly.
Upvotes: 2