0

I'm getting the error:

-su: syntax error near unexpected token '(`

This is when I run the script in shell:

psql -c CREATE TABLE test1 (device SERIAL)

If however I am already in postgres and I run the command below to create a table it works,

CREATE TABLE test (device SERIAL)

Does anyone know how I can fix the problem? Thank you

Mark
  • 3,138
  • 5
  • 19
  • 36

1 Answers1

1

Try to quote the command you want to execute.

psql -c "CREATE TABLE test1 (device SERIAL)"

or

psql -c 'CREATE TABLE test1 (device SERIAL)'

Otherwise Shell parses the command differently and tries to use every space separated token as another argument:

psql -c CREATE ...Gibberish other arguments psql does not understand...
SeleDry
  • 76
  • 3
  • Thanks, that works, what happens if I want to create multiple tables so one table as test1 and another as test2. Am I able to put a variable in there like test$i so I can do it in a for loop? – Mark Jun 29 '18 at 13:02
  • @Mark Yes, but then you need double quotes, see https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – Benjamin W. Jun 29 '18 at 13:21
  • You can create multiple tables this way, just use standard sql syntax separating commands with a semicolon `;` – SeleDry Jun 29 '18 at 13:21