4

I want to create a table with the following query:

create table something as
select 
      other_id, 
      other_title, 
      '0.0.0.0' as IP, 
      1 as used 
from
      other_table

But Postgres complains that:

column "ip" has type "unknown"

How can I specify it to be of type char?

3 Answers 3

7

You need to explicitly cast your column, like this:

'0.0.0.0'::INET as IP,
Sign up to request clarification or add additional context in comments.

1 Comment

Or ::text if you don't want to handle it as an real IP
1

At least since Postgres 9.4, this does not raise an EXCEPTION, just a WARNING. The table is created anyway, with a column of data type unknown if no type is given for the string literal:

dbfiddle for pg 9.4 here

Postgres 10 introduces more useful default behavior. String literals are cast to the default data type text unless cast explicitly. So you don't get columns of type unknown any more:

dbfiddle here

Introduced with this commit (with detailed explanation).

And the type unknown has been labeled a pseudo-type now. Details in this commit.

Comments

0

You should specify the type , use '0.0.0.0'::text AS IP

Comments

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.