Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alter a field from bigint to character varying in postgresql?

Tags:

postgresql

I get an error "incompatible types: bigint and character varying." but I know there usually is a trick to bypass this.

like image 315
johnlemon Avatar asked May 10 '11 08:05

johnlemon


People also ask

How do I change the datatype of a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

What is CHARACTER VARYING [] in PostgreSQL?

Character varying is the official type of SQL ANSI standard. It will support all SQL compliances. Character varying is most useful and important data type in PostgreSQL used without a length specifier.

What is the difference between character and CHARACTER VARYING in PostgreSQL?

The short answer: there is no difference. The long answer: CHARACTER VARYING is the official type name from the ANSI SQL standard, which all compliant databases are required to support. (SQL compliance Feature ID E021-02.) VARCHAR is a shorter alias which all modern databases also support.


2 Answers

alter table abc alter column def type varchar using def::varchar;
like image 21
Scott Marlowe Avatar answered Oct 14 '22 05:10

Scott Marlowe


Seems to work fine in PG 9.0, but if not in your version you can always convert to text first:

select 1::bigint::text::varchar;
like image 134
Denis de Bernardy Avatar answered Oct 14 '22 04:10

Denis de Bernardy