1

I have many tables in Sqlite3 db and now I want to export it to PostgreSQL, but all the time I get errors. I've used different techniques to dump from sqlite:

.mode csv

.header on

.out ddd.sql

select * from my_table

and

.mode insert

.out ddd.sql

select * from my_table

And when I try to import it through phppgadmin I get errors like this:

ERROR: column "1" of relation "my_table" does not exist

LINE 1: INSERT INTO "public"."my_table" ("1", "1", "Vitas", "[email protected]", "..

How to avoid this error?

Thanks in advance!

4
  • Apparently the table is not there ("does not exist"), so you need to create the table my_table before you can insert into it. Commented Jan 27, 2012 at 9:22
  • No, table is there - I'm sure Commented Jan 27, 2012 at 9:33
  • please show us the CREATE TABLE statement for it. Commented Jan 27, 2012 at 9:35
  • Did you try .out ddd.sql, .dump, .out stdout? Commented Jan 27, 2012 at 14:30

2 Answers 2

4

Rant

You get this "column ... does not exist" error with INSERT INTO "public"."my_table" ("1", ... - because quotes around the "1" mean this is an identifier, not literal.

Even if you fix this, the query still will give error, because of missing VAULES keyword, as Jan noticed in other answer.

The correct form would be:

INSERT INTO "public"."my_table" VALUES ('1', ...

If this SQL was autogenerated by sqlite, bad for sqlite.

This great chapter about SQL syntax is only about 20 pages in print. My advice to whoever generated this INSERT, is: read it :-) it will pay off.

Real solution

Now, to the point... To transfer table from sqlite to postgres, you should use COPY because it's way faster than INSERT.

Use CSV format as it's understood on both sides.

In sqlite3:

create table tbl1(one varchar(20), two smallint);
insert into tbl1 values('hello',10);
insert into tbl1 values('with,comma', 20);
insert into tbl1 values('with "quotes"', 30);
insert into tbl1 values('with
enter', 40);
.mode csv
.header on
.out tbl1.csv
select * from tbl1;

In PostgreSQL (psql client):

create table tbl1(one varchar(20), two smallint);
\copy tbl1 from 'tbl1.csv' with csv header delimiter ','
select * from tbl1;

See http://wiki.postgresql.org/wiki/COPY.

Sign up to request clarification or add additional context in comments.

Comments

2

Seems there is missing "VALUES" keyword:

INSERT INTO "public"."my_table" VALUES (...)

But! - You have to insert values with appropriate quotes - single quotes for text and without quotes for numbers.

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.