Syntax for adding a timestamp column in Postgres - postgresql

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.

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.

Follow the simple solution as below:
ALTER TABLE microwaves ADD COLUMN scanned_in timestamp with time zone;
For more details check PostgreSQL - ADD COLUMN

Related

Adding generated column existing table in postgresql

I'm trying to add a generated column in existing table. I've a transaction table, in that table I've one column named as dateTime(containing date and time in timestamp format). I want to create a virtual column named as transactionDate which will contain a date and time derived from dateTime column.
Below is the query which I created
ALTER TABLE public.transaction
ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS (timestamp("dateTime")::date) STORED;
and I'm getting below error:
ERROR: syntax error at or near ""dateTime""
LINE 2: ... without time zone GENERATED ALWAYS AS (timestamp("dateTime"...
^
SQL state: 42601
Character: 121
Please help me out.
What are you using timestamp() for?
For generated column, it should be like -
ALTER TABLE transaction ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS ("dateTime"::date) STORED;
OR
ALTER TABLE transaction ADD COLUMN "transactionDate" timestamp without time zone GENERATED ALWAYS AS ("dateTime"::timestamp) STORED;
Fiddle here.
The column dateTime is already a timestamp so you do not need to create a timestamp from it. So just: ( see demo)
alter table transaction
add column "transactionDate" date
generated always as ("dateTime"::date) stored;

How to add a default timestamp with time 09.00 o'clock in postgresql?

I am trying to add a default timetable into my query with a chosen time, 09.00 o'clock. The type of the column was 'date', I changed it to 'timestamp', my assignment sais so. I don't know what to do now. I am very new to this and trying to understand queries with 'timestamp'.
This is what I have so far:
ALTER TABLE note
ALTER COLUMN entered TYPE timestamp SET DEFAULT;
I don't know what to do next. Any help is appricieted!
The ALTER COLUMN allows to provide an expression for the cast through the USING keyword. As you already have a date, you can convert it to a timestamp by adding a time:
ALTER TABLE note
ALTER COLUMN entered TYPE timestamp using entered + time '09:00';
If you also want to set a default value to "today at 09:00" you can do that in the same statement:
ALTER TABLE note
ALTER COLUMN entered TYPE TIMESTAMP USING entered + time '09:00',
ALTER COLUMN entered SET DEFAULT current_date + time '09:00';

How do I convert string to time in postgresql?

I have a column of type varchar with a time on it like 120217. I would like to convert this to a time format I can query. Would something like
alter table public.table alter column col_time type date using to_date(col_time, 'HHMMSS');
work?
this should do:
alter table public.table
alter column col_time
type time using (col_time::time);

ALTER table column to timestamp now() properties

I created a table in postgres from samples I found on the internet. The definition of the column stored is:
stored | timestamp without time zone | default '2014-04-11 21:19:20.144487'::timestamp without time zone
How do I alter this to be a "normal" timestamp now() type? So it will stamp the current date-time when inserting data?
Thank you all in advance!
If you're trying to change the default value . . .
alter table your-table-name
alter column stored set default current_timestamp
For changing the field type and default value the syntax will be ...
alter table your-table-name
alter column your-field type timestamp,
alter column your-field set default current_timestamp;

Alter a Column Data Type Postgres

Hey I have just started working on PostgreSQL, and I am wondering how can we change a column's data type, I tried the following command:
alter table tableName alter column columnName type timestamp with time zone;
However I got the following message:
column "columnName" cannot be cast to type timestamp with time zone
The current column's data type is int, and i would like to change it to timestamp
Postgres doesn't know how to translate int to timestamp. There are several cases and usually they have different starting date.
Create temporary column with timestamp
Update table and copy data from old column to temporary column using your own translation
Drop old column
Rename temporary column.
If you look into documentation, you will find one line syntax with example how to convert unix time integer type:
ALTER [ COLUMN ] column [ SET DATA ] TYPE type [ USING expression ]
Postgres does't allow int type column to change directly into timezone. To achive this, you have to first change column type to varchar and then change it to timezone.
alter table tableName alter column columnName type varchar(64);
alter table tableName alter column columnName type timestamp with time zone;
There is a better way to do this, with the USING clause. Like so:
ALTER TABLE tableName
ALTER columnName type TIMESTAMP WITH TIME ZONE
USING to_timestamp(columnName) AT TIME ZONE 'America/New_York';
I achieved it from timestamp to timestamp with time zone by:
ALTER TABLE tableName ALTER COLUMN columnName SET DATA TYPE timestamp with time zone;
but if it is from timestamp to int or bigint you may need to do this:
ALTER TABLE tableName ALTER COLUMN columnName SET DATA TYPE int8 USING columnName::bigint