1

I'm a bit of a novice at bash scripting, so bear with me. I'm trying to write a script to execute a sql file using psql. From my terminal, it works fine:

psql -f /path/to/file.sql "$URI"

However, in my script I have something like this:

dbURI="postgres://some.connection.string"
psql -f /path/to/file.sql $dbURI

But I keep getting output like this:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

I cannot seem to get this to work at all. I've tried wrapping the variable in quotes, using $(command), etc, with no luck.

6
  • 1
    This might help: How to debug a bash script? Commented Feb 10, 2019 at 19:19
  • So, did you start your Postgres server? Commented Feb 10, 2019 at 19:31
  • @a_horse_with_no_name. Yes. Commented Feb 10, 2019 at 21:22
  • Are you connecting to localhost or an alias of localhost? Commented Feb 10, 2019 at 22:09
  • What do you mean with "from my terminal it works fine" - where do you run that script if not in "your terminal"? And what is the connection string that you use on "your terminal" and what is the connection string that you use on the terminal where it doesn't work? You are clearly specifying a hostname where no Postgres instance is running Commented Feb 11, 2019 at 9:15

2 Answers 2

0

Try using the below in your script to disable globbing

psql -f /path/to/file.sql "$dbURI"
Sign up to request clarification or add additional context in comments.

2 Comments

That's a sane recommendation, but if the URI in the question is representative, this is in no way going to resolve their problem.
The OP mentioned that the command works ok when run directly form the terminal (using the double quotes), but id does not from inside a script (without the double quotes) so I guess the obvious first step is to try it with the double-quotes.
0

I have just had the same problem, with the exact same error message. The problem was that Postgresql takes a few seconds to start. So, if it is the case that you start postgresql and use a psql command in the same script, chances are that postgre has not yet started when you call it.

The solution was to include:

sleep 5

before the psql command. In your case, this would be:

sleep 5
psql -f /path/to/file.sql "$URI"

This gives some time for postgre to start before you use it.

I see the topic is 2 years old, but in case anyone else faces the same problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.