views:

112

answers:

1

Hello,

I'm trying to copy the contents of one table into another in Postgres, however it appears some rows aren't being copied correctly:

ActiveRecord::StatementInvalid: PGError: ERROR: column "email_date" is of type timestamp without time zone but expression is of type character varying HINT: You will need to rewrite or cast the expression.

Is there any way I can have it automatically skip (or ignore) invalid rows?

Here's the query I'm using:

SET statement_timeout = 0; INSERT INTO emails3 SELECT * FROM emails
+1  A: 

As the message says, it's not a problem of "some invalid rows", you have incompatible types for the email_date column. Either fix that, or write the explicit conversion in the query.

UPDATE: If (as it seems) you want to copy the contents and the schema of the table (i.e. create a new table with the same schema and copy the contents), you can do it simply with a SELECT INTO.

leonbloy
Thanks, do you know if it's emails3 that has the wrong column type, or emails? Is there an easy way for me to check?I'm using Rails, and when I type "Email" into the console to view the model for the emails table, it shows that the email_date column is of type datetime. For Rails migrations and Postgres, this corresponds with "timestamp".
NudeCanalTroll
See the schema of the tables -dont you have access to a psql console? I guess the source is a character and the target (emails3.email_date) is a timestamp. But this does not say which one is "wrong".
leonbloy
Okay great. In order to ensure the two tables had a perfectly similar structure, I ended up doing this: "CREATE TABLE emails3 AS SELECT * FROM emails WHERE 1=2". After that my INSERT INTO was able to go through. Thanks for your help!
NudeCanalTroll
Ha, that was ok, but a little dirty :-) I updated the answer with a much simpler alternative to accomplish that.
leonbloy