[PYTHON] psql: I ran into an error with an invalid option --'''.

When I tried to run psql indirectly using subprocess.check_output (), I ran into the error " psql: Invalid option --''' ". I couldn't figure out what was wrong with this message alone, so I tried various things to find out the cause.

As a result, I found that there was a problem with how to create the arguments.

problem

I ran a command in Python that remotely lists the users of the PostgreSQL server.

Problem code psql01.py


#!/usr/bin/env python

import subprocess

cmd = [
    'psql',
    '-U ' + 'postgres',
    '-d ' + 'postgres',
    '-t ',
    '-c ' + 'select * from pg_user;'
]
try:
    result = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
    result = e.output

print result

Command execution (error)


[user001@localhost ~]$ python psql01.py 
['psql', '-U postgres', '-d postgres', '-t ', '-c select * from pg_user;']
psql:Invalid option-- ' '
Detail is"psql --help"Please see.

Solution

The method of creating the second argument of check_output () set in the cmd variable of the above sample code has been modified.

check_output syntax


subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

For example, when parsing a command line (with getopt etc.) in bash or C language, whitespace characters are interpreted as delimiters, but check_output () seems to have list elements as argument elements. To put it badly, check_output () doesn't seem to treat whitespace as a delimiter.

Maybe this is the correct way to use

cmd = [
    'psql',
    '-U' , 'postgres',
    '-d' , 'postgres',
    '-t',
    '-c' , 'select * from pg_user;'
]

This is OK

cmd = [
    'psql',
    '-U' + 'postgres',
    '-d' + 'postgres',
    '-t',
    '-c' + 'select * from pg_user;'
]

environment

Supplement

Recommended Posts

psql: I ran into an error with an invalid option --'''.
I get an error with import pandas.
When I get an error with PyInstaller
I got an error when saving with OpenCV
I get an error with all yum commands
I got an error when using Tensorboard with Pytorch
I got an error when I ran composer global require laravel / installer
When I get an error with Pylint in Atom on Windows
I get an error when trying to install maec 4.0.1.0 with pip
I sent an SMS with Python
I got an error when I ran meteor add accounts-password and got hooked
I got an SSL related error with pip install, so I solved it
I tried sending an SMS with Twilio
I tried sending an email with python.
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
I want to send Gmail with Python, but I can't because of an error
Let's write FizzBuzz with an error: Python Version
I want to be an OREMO with setParam!
Rollback processing when an error occurs with fabric
I tried sending an email with SendGrid + Python
I want to get an error message in Japanese with django Password Change Form
I got an error when pip install pandas on Mac, so I dealt with it