Cannot get psql command to run in bash script - bash

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.

Try using the below in your script to disable globbing
psql -f /path/to/file.sql "$dbURI"

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.

Related

Here document is cutting off commands

I'm trying to connect to my server via SSH and issue some commands to it. For some reason it seems like the commands are getting cut "off".
Here is the code that does the putty connection as well as issuing the SSH commands:
./plink.exe ${USER}#${HOSTNAME} -pw ${PASS}<<SSH
cd /some/foo/bar
deploy_artifact.sh --instance development1 some_artifact.ear
APP_development1.sh restart
exit
SSH
For me it works, but on the machine of my colleague the issued SSH commands are getting cut off and thus are not interpreted correctly. For example deploy_artifact.sh is getting turned into ploy_artifact.sh (See the following the screenshot).
How can i prevent this? And what is causing this?
Thanks in advance for any help!
It appears the problem is with the plink and how it is used. The given example sends commands as a standard input, however I did not find in the plink manual any mention that it reads commands from the STDIN. It is better to avoid undocumented features, since they may not work correctly or the author may remove them without any notice. Instead, if you want to pass commands inline you should provide them as an argument, ie you either have to use a quoted text, or you can wrap heredoc in the "$(cat *heredoc* )" code, eg:
./plink.exe ${USER}#${HOSTNAME} -pw ${PASS} "$(cat <<SSH
cd /some/foo/bar
deploy_artifact.sh --instance development1 some_artifact.ear
APP_development1.sh restart
exit
SSH
)"
Or, you can keep the commands in a file and run the plink with the -m commands_file option.

How to write bash file for establishing a ssh connection

I tried to write a bash file for establishing a SSH tunnel from Pi 1 to Pi2. I found several guides and did it according to them - so far so good. Everything is working now if I type the following in the terminal:
ssh -i .ssh/key_rsa pi#192.168.0.107
I tried the same line of code in a bash file, but there appears the following error message: „File or Dictionary not found“
My bash Code:
#!usr/bin/bash -f
xterm -title “Terminal1“ -hold -e ssh -i “.ssh/key_rsa“ “ pi#192.168.0.107“
What's the problem here?
Are you running it from inside you $HOME where .ssh/key_rsa is located?
Perhaps set it to use an absolute path like $HOME/.ssh/key_rsa

Server asking terminal type prevents me from auto-running commands

I wish to ssh into a server in my department, cd to a directory automatically, and hopefully be able to auto-run other commands if I want. I have tried several ways that have worked for lots of people on stack-overflow, but they did not work for me.
I have tried the methods from the following threads:
Can I ssh somewhere, run some commands, and then leave myself a prompt?
Run ssh and immediately execute command
In particular, I have tried:
1. ssh one-liner
ssh -t user#domain.com 'cd /some/path; bash -l'
and
2. using the expect script
#!/usr/bin/expect -f
spawn ssh $argv
send "cd /some/path\n"
interact
Both codes look fine, and have worked for people in the threads. However, they did not work for me.
The problem, I expect, lies in the fact that the department server asks me automatically about my terminal type as I login, preventing my auto-commands to be run properly.
$ ssh -t user#domain.com 'cd /some/path; bash -l'
Terminal type? [xterm-256color]
After hitting , it takes me into the home directory as if I haven't cd yet.
The second way gives a similar result.
How can I get over this? Thank you very much in advance!

Unable to run psql command from within a BASH script

I have run into a problem with the psql command in my BASH script as I am trying to login to my local postgres database and submit a query. I am using the command in the following way:
psql -U postgres -d rebasoft_appauditor -c "SELECT * FROM katan_scripts"
However, I get the following error message.
psql: FATAL: Ident authentication failed for user "postgres"
This runs perfectly fine from the command line after I appended the following changes to /var/lib/pgsql/data/pg_hba.conf:
local all all trust
host all all 127.0.0.1/32 trust
Also, could this please be verified for correctness?
I find it rather strange that database authentication works fine on the command line but in a script it fails. Could anyone please help with this?
Note: I am using MAC OSX
It might possibly depend on your bash script.
Watch for the asterisk (*) not be replaced with the file names in your current directory. And possibly a semicolon or \g might help to actually send the SQL statement to the database server.

PostgreSQL - Scripting installation from Source

I'm trying to install PostgreSQL from source and script it for automatic installation.
Installing dependances, downloading and compiling PostgreSQL works good. But there are 3 commands that I need to run as Postgres User
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
/usr/local/pgsql/bin/createdb test
I saw this link but it doesn't work in my script here is the output :
Success. You can now start the database server using:
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/
or
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start
server starting
createdb: could not connect to database template1: 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"?
admin#ip-172-31-27-106:~$ LOG: database system was shut down at 2015-03-27 10:09:54 UTC
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
And the script :
sudo su postgres <<-'EOF'
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ start
/usr/local/pgsql/bin/createdb pumgrana
EOF
After that, I need to press enter and the server is running. My database is not created. It seems like the script tries to create the database then run the server but I'm not sure. Can someone help me?
There are a few things wrong with that script:
pg_ctl should get a -w argument, making sure it waits until PostgreSQL has started before exiting.
You don't have any error checking, so it'll just keep going if something doesn't work. At minimum you should use set -e at the start.
I also suggest using sudo rather than su, which is kind of obsolete these days. You never need sudo su, that's what sudo -u is for. Using sudo also makes it easier to pass environment variables in. So I'd write something like (untested):
sudo -u postgres PATH="/usr/local/pgsql/bin:$PATH" <<-'EOF'
set -e
initdb -D /usr/local/pgsql/data/
pg_ctl -D /usr/local/pgsql/data/ -w start
createdb pumgrana
EOF
You might want to pass PGPORT or some other relevant env vars into the script too.
Completely separately to this ... why? Why do this? If you're automating an install from source, why not just build a .deb or .rpm automatically instead, then install that?

Resources