I'm writing a script that is called locally to run psql queries on a remote server. I'm writing the script in Python 2.7 using the subprocess module. I'm using subprocess.Popen to ssh into the remote server and directly run a psql command. My local machine is osx and I think the server is CentOS.
When I call my script locally, I get back an error saying psql: command not found
. If I run the same exact psql command on the remote server, then I get back the correct query result.
I suspected there might be something wrong with my ssh code, so instead of sending over psql commands to the remote server, I tried sending over simple commands like ls
, cd
, mkdir
, and even scp
. All of those worked fine, so I don't think there's a problem with my code that ssh's over the commands to the remote server.
Does anybody understand what is going wrong with my code and why I'm getting back psql: command not found
? I researched and found this earlier SO question, but psql is installed on the remote server as evidenced by the fact that I can run psql commands manually on there.
Postgresql -bash: psql: command not found
From an external class file:
def db_cmd(self, query):
# check that query ends in semicolon
if query[-1] != ';':
query = query + ';'
formatted_query = 'psql ' + self.db_string + \
' -c "begin; ' + query + ' ' + self.db_mode + ';"'
print formatted_query
ssh = subprocess.Popen(['ssh', self.host, formatted_query],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if not result:
error = ssh.stderr.readlines()
print >>sys.stderr
for e in error:
print 'ERROR: %s' % e,
else:
for r in result:
print r,
Excerpt from a local script that imports above class and sends out the psql command to remote server:
s = Jump(env, db_mode)
s.db_cmd('select * from student.students limit 5;')
Running my script locally.
Note that the script prints out the psql command for debugging. If I copy and paste that same psql command and run it on the remote server, I get back the correct query result.
$ ./script.py 1 PROD ROLLBACK
psql -h <server_host> -d <db_name> -U <db_user> -p <port> -c "begin; select * from student.students limit 5; ROLLBACK;"
ERROR: bash: psql: command not found
Thanks