5

I have been developing locally for some time and am now pushing everything to production. Of course I was also adding data to the development server without thinking that I hadn't reconfigured it to be Postgres.

Now I have a SQLite DB who's information I need to be on a remote VPS on a Postgres DB there.

I have tried dumping to a .sql file but am getting a lot of syntax complaints from Postgres. What's the best way to do this?

5
  • Could use an intermediate format like CSV? Although "just inserts" in the .sql dump should be okay if the Pg schema matches .. verify there is no DDL/DML in the .sql dump? Also, consider posting the part of the dump that generates the syntax error in Pg, along with the exact error message(s) .. Commented Aug 18, 2012 at 18:37
  • DDL/DML? It's complaining about AUTOINCREMENT Commented Aug 18, 2012 at 18:39
  • Also some PRAGMA that I think I can safely delete Commented Aug 18, 2012 at 18:40
  • DDL = Data Definition Language = CREATE TABLE. (Forget what I said about DML in that context.) Just make sure Pg has the same schema -- Pg would use a sequence for the "autoincrement" column -- and then make sure the import only contains DML (e.g INSERT). Commented Aug 18, 2012 at 18:40
  • github.com/dimitri/pgloader Commented Nov 7, 2018 at 9:25

3 Answers 3

4

For pretty much any conversion between two databases the options are:

  1. Do a schema-only dump from the source database. Hand-convert it and load it into the target database. Then do a data only dump from the source DB in the most compatible form of SQL dump it offers. Try loading that into the target DB. When you hit problems, script transformations to the dump using sed/awk/perl/whatever and try again. Repeat until it loads and the results match.

  2. Like (1), hand-convert the schema. Then write a script in your preferred language that connects to both databases, SELECTs from one, and INSERTs into the other, possibly with some transformations of data types and representations.

  3. Use an ETL tool like Talend or Pentaho to connect to both databases and convert between them. ETL tools are like a "somebody else already wrote it" version of (2), but they can take some learning.

  4. Hope that you can find a pre-written conversion too. Heroku one called sequel that will work for SQLite -> PostgreSQL; is it available without Heroku and able to function without all the other Heroku infrastructure and code?

After any of those, some post-transfer steps like using setval() to initialize sequences is typically required.

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

Comments

3

Heroku's database conversion tool is called sequel. Here are the ruby gems you need:

gem install sequel
gem install sqlite3
gem install pg

Then this worked for me for a sqlite database file named 'tweets.db' in the current working directory:

sequel -C sqlite://tweets.db postgres://pgusername:pgpassword@localhost/pgdatabasename

2 Comments

Thanks! Tip for anyone googling this: I had to install gem sqlite3 instead of sqlite. Otherwise you get: Error: Sequel::AdapterNotFound: LoadError: cannot load such file -- sqlite3 <internal:/usr/local/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in 'require'
Thank you @hraban , I updated the gem installs.
1

PostgreSQL supports "foreign data wrappers", which allow you to directly access any data source through the DB, including sqlite. Even up to automatically importing the schema. You can then use create table localtbl as (select * from remotetbl) to get your data into the actual PG storage.

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.