I just discovered that I can insert values of any type into a PostgreSQL (9.6) column of type text
:
drop table if exists d cascade;
create table d ( a text );
insert into d values ( 42 );
insert into d values ( true );
select a, pg_typeof( a ) from d;
a | pg_typeof
------+-----------
42 | text
true | text
(2 rows)
Is this an intentional feature? Have I done something wrong? Is there a setting to avoid this? Doesn't this violate
the assumption that RDBMSs should be typesafe?
I know that text
acts like a catch-all in PostgreSQL which is often convenient because you can then write
string representations of arbitrary types. But surely sometimes you want to make sure that only strings get
inserted into a given column, to the exclusion of implicitly cast values.
Is there anything I can do to avoid 'casual' type casts?
Best Answer
Yes, implicit type conversion is an intentional feature. There are pros and cons either way but this is how postgres is designed to work:
No, everything is still typesafe and "SQL is a strongly typed language". Implicit conversions don't change that.
Short of messing around with the system catalog, which is usually a very bad idea (and impossible in some cases), there isn't a lot you can do to avoid implicit casting entirely. Even if you include explicit casts everywhere implicit casts might still occur by mistake:
dbfiddle here