ALTER TABLE user_login ALTER COLUMN dob date;
Is this correct query in psql to modify the data type of the column?
No, you need to include TYPE. It should be
ALTER TABLE user_login ALTER COLUMN dob TYPE date
See the documentation. You're probably confused with ADD COLUMN where you can omit the TYPE.
Related
I have a PostgresSQL table that has a Date column of type TEXT
Values look like this: 2019-07-19 00:00
I want to either cast this column to type DATE so I can query based on latest values, etc.... or create a new column of type DATE and cast into there (so I have both for the future). I would appreciate any advice on both options!
Hope this isn't a dupe, but I havn't found any answers on SO.
Some context, I will need to add more data later on to the table that only has the TEXT column, which is why i want to keep the original but open to suggestions.
You can alter the column type with the simple command:
alter table my_table alter my_col type date using my_col::date
This seems to be the best solution as maintaining duplicate columns of different types is a potential source of future trouble.
Note that all values in the column have to be null or be recognizable by Postgres as a date, otherwise the conversion will fail.
Test it in db<>fiddle.
However, if you insist on creating a new column, use the update command:
alter table my_table add my_date_col date;
update my_table
set my_date_col = my_col::date;
Db<>fiddle.
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);
I have a table with a text column representing a MAC address, but I want to use the macaddr type instead. So, I tried:
alter table mytable alter column mac_column type macaddr;
And got this error
ERROR: column "mac_column" cannot be cast automatically to type macaddr
HINT: Specify a USING expression to perform the conversion.
But I don't know what to use as USING expression:
alter table mytable alter column mac_column type macaddr using(????????)
What should I use as USING expression?
Many thanks in advance
You can’t simply change the data type because data is already there in the column. Since the data is of type String PostgreSQL can't expect it as macaddr though you entered valid String representation of the macaddr. So now, as PostgreSQL suggested you can use the ‘USING’ expression to cast your data into macaddr.
ALTER TABLE mytable ALTER COLUMN mac_column TYPE macaddr USING(mac_column::macaddr)
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