Sequel is a fantastic ORM available in Ruby.

It also comes with an IRB console so it can be run directly from the command line with various options.

For example, to console into an SQLite database testdb.sqlite in the current directory –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$> sequel sqlite://testdb.sqlite
$> sequel sqlite://testdb.sqlite
$> sequel sqlite://testdb.sqlite

Or for Postgres –

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$> sequel postgres://dbuser:dbpassword@localhost/testdb
$> sequel postgres://dbuser:dbpassword@localhost/testdb
$> sequel postgres://dbuser:dbpassword@localhost/testdb

However, I use sequel most frequently to take quick database backups or to restore them.

Copy Postgres to SQLite

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$> sequel -C postgres://localhost/testdb sqlite://testdb.sqlite
$> sequel -C postgres://localhost/testdb sqlite://testdb.sqlite
$> sequel -C postgres://localhost/testdb sqlite://testdb.sqlite

Copy SQLite to Postgres

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$> sequel -C sqlite://testdb.sqlite postgres://localhost/testdb
$> sequel -C sqlite://testdb.sqlite postgres://localhost/testdb
$> sequel -C sqlite://testdb.sqlite postgres://localhost/testdb

Copy SQLite to SQLite

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$> sequel -C sqlite://testdb.sqlite sqlite://testdb-copy.sqlite
$> sequel -C sqlite://testdb.sqlite sqlite://testdb-copy.sqlite
$> sequel -C sqlite://testdb.sqlite sqlite://testdb-copy.sqlite

Note:

  • The target database should be an empty database i.e. it should not have any tables
  • Path to SQLite should be fully qualified path / current directory
  • If Postgres requires credentials, they should be in the connection parameters –
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres://databaseuser:databasepassword@localhost/testdb
postgres://databaseuser:databasepassword@localhost/testdb
postgres://databaseuser:databasepassword@localhost/testdb