0

Suppose that one of the tables in PostgreSQL 10 database is called 'Measurement' and has (among others) a column called 'Name' of text type.

I would like to list all records with specific name, so sql statement:

SELECT * FROM public."Measurement" WHERE "Measurement"."Name" = 'nazwa1';

works fine in pgAdmin SQL editor.

I would like to ask server about the same but with psql working in command line on Ubuntu, something like this:

psql --host 127.0.0.1 --dbname BazaDanych --username postgres --port 5432 --echo-all --no-align --command 'SELECT * FROM public."Measurement" WHERE "Measurement"."Name" = 'nazwa1';' --log-file /home/user/wynik.log -o /home/user/dane.csv -P fieldsep=',' -P footer='off'

It does not work since single quotation mark from this statement is not recognized by shell and it causing problem reported as "column does not exist",as follows:

public."Measurement" WHERE "Measurement"."Name" = nazwa1

'nazwa1' string is not interpreted as a value to be compared with the records but as a column name.

I have tried many ways, as: \', doubling single quotation mark and many others but with no successful results.

How to workaround this ? Could someone correct my command ?

1 Answer 1

4

Fortunately, I found it!

Two steps are needed:

  1. Replace external single quotation marks to quotation mark (' -> ")
  2. Add \ before internal quotation marks (" -> \")

So, code below works fine:

psql --host 127.0.0.1 --dbname BazaDanych --username postgres --port 5432 --echo-all --no-align --command "SELECT * FROM public.\"Measurement\" WHERE \"Measurement\".\"Name\" = 'nazwa1';" --log-file /home/user/wynik.log -o /home/user/dane.csv -P fieldsep=',' -P footer='off'

It might be helpful to someone. If not, admins, please delete my post.

Sign up to request clarification or add additional context in comments.

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.