1

I've tried this a few different ways but every time I get a syntax error when trying to add columns to a table.

I'm new to databases so it's probably something basic that I'm doing wrong, but I just don't see why this isn't working.

Any input would be appreciated.

P.S. at the end of the query line there is a ";"... You just can't see it in the screenshot.

So far I've tried changing "add columns" to "add column" and just "add" followed by the columns. I'm aware I can just use the GUI to copy columns in DBeaver but I'd rather get this SQL working.

enter image description here

1

According to Postgres docs the correct syntax is:

ALTER TABLE <your_table>
    ADD COLUMN <column name> <column type>,
    ADD COLUMN <column name> <column type>,
    ADD COLUMN <column name> <column type>;

Have a look at Postgres tutorials on PostgreSQL ADD COLUMN: Add One Or More Columns To a Table.

I've set up a minimal example that reproduces your error:

CREATE TABLE newtable (id int);

ALTER TABLE newtable
    ADD COLUMNS f1 float8, f2 int; 

ERROR: syntax error at or near "float8"
LINE 2: ADD COLUMNS f1 float8, f2 int; ^

ALTER TABLE newtable
    ADD COLUMN f1 float8, f2 int, f3 text;

ERROR: syntax error at or near "f2"
LINE 2: ADD COLUMN f1 float8, f2 int, f3 text;

Using the correct syntax:

ALTER TABLE newtable
    ADD COLUMN f1 float8,
    ADD COLUMN f2 int,
    ADD COLUMN f3 text;
✓

db<>fiddle here

  • Thanks McNets, using the correct syntax at the bottom of your post worked. From what I read on other posts I was under the impression that you could add multiple columns without multiple "add column" statements using a syntax like: ALTER TABLE newtable ADD COLUMN (column1 float8, column2 int, etc.);... but I don't see that anywhere in the official docs. Thanks again! – gulfy Jan 13 at 10:14
  • I'm glad to help. – McNets Jan 13 at 10:15

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.