Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a shell script thats generated by a java jar that I am unable to run anything postgres related when its created. However, when copying all of its content into a new shell script file, I am able to run the script flawlessly. The content of the script is basically just connecting to my postgres database, creating a new database and populating it. Here's a snippet:

#!/bin/bash

export DATABASE_NAME="dbname"
export DB_USERNAME="postgres"
psql -c "drop database $DATABASE_NAME;" postgres postgres
psql -c "create database $DATABASE_NAME;" postgres postgres

echo "Starting scripts"
psql -f db/pgsql/schema/schema.sql $DATABASE_NAME $DB_USERNAME

Any ideas on why this is the case?

EDIT: For clarification, the script errors when it reaches this line:

psql -c "drop database $DATABASE_NAME;" postgres postgres

It is giving the following error for every line that needs to be executed in this file:

psql: fe_sendauth: no password supplied

Things I've confirmed:

  • Shell script is executable

  • ~/.pgpass has the necessary credentials

  • If copying contents of this script to a new file and I run that file, everything runs as expected.

share|improve this question
    
What error do you get when trying to execute the original shell script? –  Flimzy Jun 17 '11 at 0:35
    
Added error into original question as "psql: fe_sendauth: no password supplied" –  Sonny Jun 17 '11 at 0:52
    
Can you verify is the file owner same for both java generated script and the newly copied script? –  bbaja42 Jun 17 '11 at 6:02
add comment

3 Answers

up vote 1 down vote accepted

I would use diff and cat -v filename to see what the differences are between the working and non-working scripts. Assuming the second "postgres" is a password, there may be something invisible that is preventing it being recognised - for example a Windows control-M (CR) line ending being used on a *Nix machine.

share|improve this answer
    
Thanks! I did a cat-v filename on both files and I did find an extra character at the end of the non-working file. The character in question is ^m, which (with a quick google search), looks to be a windows control-M. –  Sonny Jul 7 '11 at 1:02
add comment

Make sure that the shell script file that is created has the execute bit set before you run it.

share|improve this answer
    
Just to clarify, do you mean make sure its executable? If that is the case, yes I changed it so that it is (chmod 755) and still no avail. –  Sonny Jun 17 '11 at 0:38
    
Does the script run if you change the permissions to 777? –  Adam Prax Jun 17 '11 at 0:45
    
Sorry Adam, changing permission to 777 still gave the password error (added into original question) when it tried executing postgres-related commands. Please disregard previous comment –  Sonny Jun 17 '11 at 0:57
add comment

with info from postgres wiki and psql --help I'm suggesting that you put your username in the .pgpass file and run the following in the script:

export PGPASSFILE=$HOME/.pgpass
psql -w -f db/pgsql/schema/schema.sql $DATABASE_NAME 

PS: are you running the newly copied file by sourcing it or by running it as a binary? i.e:

. newfile 

OR

./newfile
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.