Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for adding a timestamp column in Postgres

After research on here I wanted to use "timestamp with time zone" but cannot figure out the proper syntax based on the postgres docs.

ALTER TABLE microwaves ADD COLUMN scanned_in DATA_TYPE timestamp with time zone;

ALTER TABLE microwaves ADD COLUMN scanned_in TYPE timestamp with time zone;

Both throw errors.

Any help appreciated, thanks.

like image 967
some1 Avatar asked Aug 18 '16 23:08

some1


People also ask

How do I create a timestamp column in PostgreSQL?

PostgreSQL timestamp example First, create a table that consists of both timestamp the timestamptz columns. Next, set the time zone of the database server to America/Los_Angeles . After that, query data from the timestamp and timestamptz columns. The query returns the same timestamp values as the inserted values.

How do I add a column in PostgreSQL?

The syntax to add a column in a table in PostgreSQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ADD new_column_name column_definition; table_name. The name of the table to modify.

Does Postgres support timestamp?

PostgreSQL supports the full set of SQL date and time types, shown in Table 8.9.


1 Answers

You just had the syntax wrong. You don't need the [DATA] TYPE part here (that's only needed when you want to change the type) :

CREATE TABLE barf
        ( id serial PRIMARY KEY);

ALTER TABLE barf ADD COLUMN scanned_in timestamp with time zone;

BTW (just a hint): most of the ALTER syntax just mimics the syntax for CREATE TABLE (...): the sub-syntax is mostly the same.

like image 183
wildplasser Avatar answered Oct 01 '22 21:10

wildplasser