14

I want to cast a specific column in my PostgreSQL database from character_varying type to type date. phpPgAdmin gives me the following error:

ERROR: column "date" cannot be cast to type date

In Statement: ALTER TABLE "public"."tablename" ALTER COLUMN "date" TYPE date

What should I do ? Thanks

1 Answer 1

41

You might need to convert it to text first:

alter table "foo" alter column "date" type date using ("date"::text::date);
Sign up to request clarification or add additional context in comments.

6 Comments

Hi and thanks for the answer. I have another problem. The date column is used in one of my views so i get the following error: ERROR: cannot alter type of a column used by a view or rule How can i overcome this ?
In that case you cannot: you need to drop the view and recreate it.
If you want to save or see the definition of the view, it's stored in pg_views btw.
How can I achieve this if my column has dates saved as dd/mm/yyyy. I get this error message with your solution. PG::DatetimeFieldOverflow: ERROR: date/time field value out of range: "27/11/1992" HINT: Perhaps you need a different "datestyle" setting.
@acrogenesis: look into the to_date() function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.