Skip to content
Advertisement

Can’t update dictionary’s column in postgres DB because of double quotes?

I can update the column in question in two CLI commands:

psql -U postgres
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';

That works just fine, but I am going to running the commands in python and doing something like subprocess.check_output(cli_0 + ” & ” + cli_1, shell=True) results in thinking the second is a bash command. I would ideally like to run something like:

psql -U postgres -c "UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';"

But the double quotes in the dictionary kinda kills that idea. Turning them into single quotes also doesn’t work. Any suggestions would be incredibly helpful. I have access to python3.5 and I can’t pip install anything.

Advertisement

Answer

You should be able to backslash escape the inner double-quotes:

psql -U postgres -c "UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';"

Otherwise, you can save to file and run it:

cat <<_EOF_ > /tmp/myfile
> UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
> _EOF_
psql -U postgres < /tmp/myfile

You should also be able to do a heredoc directly into psql:

psql postgres postgres <<_EOF_
> select 1;
> _EOF_
 ?column? 
----------
        1
(1 row)
psql U postgres <<_EOF_
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';
_EOF_
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement