0

I am trying Run psql query and get output ERROR: syntax error at or near "$"

Run query from psql

 ➤ psql://postgres@dev-postgresql-95-1:5432/mydb 

# CREATE FUNCTION f_showfile(myfile text) RETURNS text AS $x$ BEGIN RETURN pg_read_file(myfile, 0, 100000); END; $x$ LANGUAGE PLPGSQL SECURITY DEFINER;
CREATE FUNCTION

Run query from bash

-bash-4.2$ psql -d mydb -c "CREATE FUNCTION f_showfile(myfile text) RETURNS text AS $x$ BEGIN RETURN pg_read_file(myfile, 0, 100000); END; $x$ LANGUAGE PLPGSQL SECURITY DEFINER;"
ERROR:  syntax error at or near "$"
LINE 1: ... FUNCTION f_showfile(myfile text) RETURNS text AS $ BEGIN RE...

How correct run psql query in bash? Thank you

3

The string you send contains the following :

$x$

which is expanded by bash as the content of the variable $x. As $x is empty, $x$ is replaced by $. You can avoid the variable expansion by using simple quotes instead of double quotes :

psql -d mydb -c 'CREATE FUNCTION f_showfile(myfile text) RETURNS text AS $x$ BEGIN RETURN pg_read_file(myfile, 0, 100000); END; $x$ LANGUAGE PLPGSQL SECURITY DEFINER;'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.