ALTER table column to timestamp now() properties - postgresql

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;

Related

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 to add default value 0000-00-00 to date datatype in postgresql?

I previously changed data type of my column by using below command but now I want to add default value like 0000-00-00 for the same column, can any one help me?
alter table table name alter column name type date using(column name::date)
You have an issue if the column is a date because 0000-00-00 is not a valid date. The syntax for setting the default is:
alter table t alter column col set default '0001-01-01';
However, you need a valid date for that. I would recommend just using NULL if that works for your application.

Add timestamp column with default NOW() for new rows only

I have a table that has thousands of rows. Since the table wasn't constructed with created_at column initially, there is no way of getting their creation timestamp. It is crucial though to start getting the timestamps for future rows.
Is there a way I can add a timestamp column with default value NOW() so that it won't populate the values to previous rows but only for the future ones?
If I do the ALTER query, it populates all rows with timestamp:
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
You need to add the column with a default of null, then alter the column to have default now().
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
You could add the default rule with the alter table,
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
then immediately set to null all the current existing rows:
UPDATE mytable SET created_at = NULL
Then from this point on the DEFAULT will take effect.
For example, I will create a table called users as below and give a column named date a default value NOW()
create table users_parent (
user_id varchar(50),
full_name varchar(240),
login_id_1 varchar(50),
date timestamp NOT NULL DEFAULT NOW()
);
Thanks
minor optimization.
select pg_typeof(now()); --returns: timestamp with time zone. So now include timezone.
So better with timestamptz.
begin;
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMPTZ;
ALTER TABLE mytable ALTER COLUMN created_at SET DEFAULT now();
commit;
Try something like:-
ALTER TABLE table_name ADD CONSTRAINT [DF_table_name_Created]
DEFAULT (getdate()) FOR [created_at];
replacing table_name with the name of your table.

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.
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

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