4

I have a SQLite3 database. I did a data dump which looks something like this:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "admin_tools_menu_bookmark" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL,
    "url" varchar(255) NOT NULL,
    "title" varchar(255) NOT NULL
);
INSERT INTO "admin_tools_menu_bookmark" VALUES(1,2,'/admin/recipes/recipe/','Recipe Management');
INSERT INTO "admin_tools_menu_bookmark" VALUES(2,2,'/admin/recipes/ingredient/','Ingredient Management');
CREATE TABLE "admin_tools_dashboard_preferences" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL,
    "data" text NOT NULL
);
......

I'm trying to execute this in PostgreSQL PgAdmin III, which gives me many many errors starting with PRAGMA, to 'unsigned' fields to datetime fields to 1 instead of true and 0 instead of false.

Is there a proper way to convert this script?

I thought of exporting each table to CSV then importing them into a PGDB but I have so many tables this isn't an option.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
darren
  • 18,845
  • 17
  • 60
  • 79
  • What did you use to create this dump? According to [this](http://www.sqlite.org/sqlite.html) - section #Converting An Entire Database To An ASCII Text File# - the '.dump' command creates an output that should be compatible (aka standard SQL) with other databases. – Mel Jun 01 '11 at 16:45
  • Possible duplicate of [Convert SQLITE SQL dump file to POSTGRESQL](http://stackoverflow.com/questions/4581727/convert-sqlite-sql-dump-file-to-postgresql) – franklin Mar 11 '16 at 04:28

4 Answers4

3

Ruby solution:

gem install sequel

sequel -C sqlite://db/development.sqlite3 postgres://user:password@localhost/dbname
Mikhail Chuprynski
  • 2,404
  • 2
  • 29
  • 42
0

Use a tool that can copy data between databases like SquirrelSQL.

There is a JDBC driver for sqlite3.

Note that this will still be a lot of manual work. The JDBC drivers will allow to copy the data but you still need to convert the table definitions manually.

Or you can write a tool that does simple string replacement; this usually works pretty well with SQL.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

I suppose PRAGMA foregin_keys... is some SQLite syntax which is not working in PostgreSQL.
Can you simply skip it and make:

BEGIN TRANSACTION;
CREATE TABLE "admin_tools_menu_bookmark" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL,
    "url" varchar(255) NOT NULL,
    "title" varchar(255) NOT NULL
);
INSERT INTO "admin_tools_menu_bookmark" VALUES(1,2,'/admin/recipes/recipe/','Recipe Management');
INSERT INTO "admin_tools_menu_bookmark" VALUES(2,2,'/admin/recipes/ingredient/','Ingredient Management');
CREATE TABLE "admin_tools_dashboard_preferences" (
    "id" integer NOT NULL PRIMARY KEY,
    "user_id" integer NOT NULL,
    "data" text NOT NULL
);
COMMIT;  

I tested it in my pgAdmin III and works.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mj82
  • 5,193
  • 7
  • 31
  • 39
  • my sql has over 15000lines, with many parts that dont' work like unsigned, datetime, etc... – darren Jun 01 '11 at 16:10
0

I used a program called Navicat. I exported each table and then imported them into the new database, which was painful and slow.

The SQL dump using SQLiteman just didn't bode well when I put the SQL into PG Admin III.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
darren
  • 18,845
  • 17
  • 60
  • 79