I am attempting to add a column to a table in postgres version 9.6.2.
$ psql --version
psql (PostgreSQL) 9.6.2
Accordingly, I am referencing the ALTER TABLE documentation for postgres 9.6.
The documentation says:
ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ] action [, ... ]
where action is one of:
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
I have a table task:
=> select * from task;
id | name
----+------
(0 rows)
on which I want to insert a column state using an idempotent command (i.e. it checks for whether the state column has been created already). That command is:
ALTER TABLE task ADD COLUMN IF NOT EXISTS state BIGINT NOT NULL;
However, this gives a syntax error at NOT:
=> ALTER TABLE task ADD COLUMN IF NOT EXISTS state BIGINT NOT NULL;
ERROR: syntax error at or near "NOT"
LINE 1: ALTER TABLE task ADD COLUMN IF NOT EXISTS state BIGINT NOT N...
Is this command inconsistent with the documentation? How do I resolve the syntax error?
Note: The command works without error when I remove the IF NOT EXISTS phrase, but the resulting command in that case is not idempotent as desired.
select version();say? Just becausepsqlis from 9.6.2 doesn't mean that the server you're connecting to is 9.6.2.select version()showed the version to be 9.4, and the documentation for 9.4 does not show a IF NOT EXISTS option for created columns. It seems I'll have to think of how to make the command idempotent without that option.