0

In the psql shell . It working fine to run sql . But when i use the psql -c . It's has problem.

postgres@03e0948a4fed:/home/data$ psql --c "select version();"
                                                             version
----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 11.4 (Debian 11.4-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
(1 row)
postgres@03e0948a4fed:/home/data$ psql
psql (11.4 (Debian 11.4-1.pgdg90+1))
Type "help" for help.

postgres=# truncate table "TEST"."E0005";
TRUNCATE TABLE

postgres@03e0948a4fed:/home/data$ psql --c "truncate" "table" "TEST"."E0005";
psql: FATAL:  role "TEST.E0005" does not exist

postgres@03e0948a4fed:/home/data$ psql --c "truncate table TEST.E0005;" `ERROR: schema "TEST" does not exist.`
5
  • I want to know how to use psql --c to run the sql truncate table TEST.E0005; Commented Jul 18, 2019 at 2:09
  • What's meaning "psql: FATAL: role "TEST.E0005" does not exist ?" Commented Jul 18, 2019 at 2:29
  • The command you put at the end is close, with the full command in quotes, but it should be -c instead of --c Commented Jul 18, 2019 at 2:34
  • @Jeremy Thank you !! I tied to use '-c' postgres@03e0948a4fed:/$ psql -c "truncate table TEST.E0005;" ERROR: schema "TEST" does not exist . What's the problem ? Commented Jul 18, 2019 at 3:09
  • The parameter should be -c not --c Commented Jul 18, 2019 at 5:42

2 Answers 2

2

There are two problems:

  1. The argument to -c bust be a single string
  2. You cannot nest the same quotes without escaping them.

So you have two solutions:

psql -c 'TRUNCATE TABLE "TEST"."E0005"'

or

psql -c "TRUNCATE TABLE \"TEST\".\"E0005\""
Sign up to request clarification or add additional context in comments.

Comments

1

To add to Laurenz Albe's answer, you could also pass the command through standard input without worrying about quotes.

psql <<eof
  TRUNCATE TABLE "TEST"."E0005"
eof

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.