1

I was trying to break a long command line involving psql command string (i.e. psql -c),and this seems to cause errors. For example, with PostgreSQL 9.5 and Ubuntu 16.04:

$ psql -c "\\dt"

works fine, while

$ psql -c "
> \\dt
> "

generates:

ERROR:  syntax error at or near "\"
LINE 2: \dt

Just out of curiosity, when is it OK to insert newlines (i.e. \n) into a psql command string?

thor
  • 21,418
  • 31
  • 87
  • 173

1 Answers1

1

command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single backslash command.

https://www.postgresql.org/docs/current/static/app-psql.html

It seems that psql does not understand a backslash command with the leading new line.

As an alternative you can use piped echo command, also described in the documentation. For example:

$ echo '
> \d
> select 1 as x;' | psql postgres

        List of relations
 Schema | Name  | Type |  Owner   
--------+-------+------+----------
 public | dummy | view | postgres
(1 row)

 x 
---
 1
(1 row)
Abelisto
  • 14,826
  • 2
  • 33
  • 41