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
Related
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';
I'm quite new to PostgreSQL. I've looked at other existing threads, the documentation and various google searches but still can't fully understand if it is possible to convert existing date values that are actually strings in a varchar column into an actual date format.
The column is named datelaid and has a varchar datatype. The table is named init_sa_mains_1
datelaid:
" "
"01-12-2011"
"01-12-2011"
" "
"01-12-2011"
" "
I have tried to change the data type of the column as follows, with no success:
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date USING datelaid::date;
ALTER TABLE init_sa_mains_1
CAST(init_sa_mains_1.datelaid as date);
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid USING datelaid::date;
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date;
USING to_date(datelaid, 'DD-MM-YYYY');
Could anyone please advise on a possible solution?
You will need to deal with empty strings as they can't be converted like that. But you can turn them into NULL values:
ALTER TABLE init_sa_mains_1
ALTER COLUMN datelaid TYPE date;
USING to_date(nullif(trim(datelaid),''), 'DD-MM-YYYY');
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);
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
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;