How to use pgloader to migrate sqlite database to postgresql

I needed to migrate a Kotti database, from its default sqlite file store, to Postgresql. Clued in by StackOverflow, I've tried using pgloader, but the version coming with Ubuntu is old: 2.x instead of the brand new 3.x. But the jump to 3.x meant a switch in programming languages as well: the new one is written in Lisp. I didn't want to install and compile the whole Lisp bundle just to run pgloader and I didn't find a binary distribution either, and after a recent exposure to Docker, I thought I'll give the dockerized version of pgloader a try.

After following the steps to install Docker, took me a bit to figure out the process (note: I'm running all this in a VMWare virtual machine, so I can afford taking a lot of unsecure shortcuts):

First, the local Postgresql database needs to be configured to run on an IP, to allow the dockerized pgloader process to connect to it.

sudo vim  /etc/postgresql/9.3/main/pg_hba.conf

Change the network settings to allow connections from all:

# IPv4 local connections:
host    all             all             0.0.0.0/0               md5
Also, we will need to enable listening on TCP connections:
sudo vim  /etc/postgresql/9.3/main/postgresql.conf
and add a listen_addresses line
listen_addresses = '*'		# what IP address(es) to listen on;
Install Dockerized pgloader according to instructions:
docker pull dimitri/pgloader

I've created a file named "convert" with the commands for pgloader, to do the conversion:

load database  
    from 'mydb.db'  
    into postgresql://user:password@192.168.1.20/mydb

with reset sequences, create no tables, include no drop, create no indexes, disable triggers
set work_mem to '200MB', maintenance_work_mem to '512 MB';

For the IP of the postgresql I've used the one attached to eth0.

Because I didn't trust pgloader to convert all the nuances of the database relations, before running the conversion, I've started once Kotti, bin/pserve app.ini so that it will create the initial database structure. After that, truncated the nodes table so that it will erase most of the database content:
psql mydb
truncate table nodes cascade
truncate table nodes principals
Now, the trick is to put this 'convert' file, together with the 'mydb.db' sqlite file in the same folder and map that folder as a volume when running the pgloader command:
sudo docker run --rm --name pgloader -v /path/to/data/:/data dimitri/pgloader:latest pgloader /data/convert

Comments