DavidHyogo
DavidHyogo

Reputation: 2898

Can't run psql from an ant target running in NetBeans 8.0.2

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

Answers (1)

DavidHyogo
DavidHyogo

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:

  1. Run cmd.exe not psql directly
  2. Set addsourcefile to true because I want to call neat little self-contained SQL files for each target.
  3. Use apply with a filelist because for my createtables or populate targets I may have a directory of separate SQL files
  4. Give up on hoping to invoke the pgpass.conf file. For some reason Ant in NetBeans can't access it. Resort to the PGPASSWORD environment key and set it to the desired password. This is running locally for development purposes so security isn't an issue.
  5. You need to pass the /c switch to cmd.exe so that you can then pass a command line. I didn't separate the psql command line into separate arguments because I think the complete line is being passed to cmd.exe as a single argument.
  6. addsourcefile is set to true so each file in the filelist is appended to the psql command line just after the -f switch and everything works a treat.

Voilà! What a fuss! I had no similar difficulty with MySql because you can pass the password to the command line directly.

Upvotes: 2

Related Questions