Package: db-to-sqlite
db-to-sqliteCLI tool for exporting tables or queries from any SQL database to a SQLite file.InstallationInstall from PyPI like so:pip install db-to-sqliteIf you want to use it with MySQL, you can install the extra dependency like this:pip install 'db-to-sqlite[mysql]'Installing themysqlclientlibrary on OS X can be tricky - I've foundthis recipeto work (run that before installingdb-to-sqlite).For PostgreSQL, use this:pip install 'db-to-sqlite[postgresql]'UsageUsage: db-to-sqlite [OPTIONS] CONNECTION PATH

  Load data from any database into SQLite.

  PATH is a path to the SQLite file to create, e.c. /tmp/my_database.db

  CONNECTION is a SQLAlchemy connection string, for example:

      postgresql://localhost/my_database
      postgresql://username:passwd@localhost/my_database

      mysql://root@localhost/my_database
      mysql://username:passwd@localhost/my_database

  More: https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls

Options:
  --version                     Show the version and exit.
  --all                         Detect and copy all tables
  --table TEXT                  Specific tables to copy
  --skip TEXT                   When using --all skip these tables
  --redact TEXT...              (table, column) pairs to redact with ***
  --sql TEXT                    Optional SQL query to run
  --output TEXT                 Table in which to save --sql query results
  --pk TEXT                     Optional column to use as a primary key
  --index-fks / --no-index-fks  Should foreign keys have indexes? Default on
  -p, --progress                Show progress bar
  --postgres-schema TEXT        PostgreSQL schema to use
  --help                        Show this message and exit.For example, to save the content of theblog_entrytable from a PostgreSQL database to a local file calledblog.dbyou could do this:db-to-sqlite "postgresql://localhost/myblog" blog.db \
    --table=blog_entryYou can specify--tablemore than once.You can also save the data from all of your tables, effectively creating a SQLite copy of your entire database. Any foreign key relationships will be detected and added to the SQLite database. For example:db-to-sqlite "postgresql://localhost/myblog" blog.db \
    --allWhen running--allyou can specify tables to skip using--skip:db-to-sqlite "postgresql://localhost/myblog" blog.db \
    --all \
    --skip=django_migrationsIf you want to save the results of a custom SQL query, do this:db-to-sqlite "postgresql://localhost/myblog" output.db \
    --output=query_results \
    --sql="select id, title, created from blog_entry" \
    --pk=idThe--outputoption specifies the table that should contain the results of the query.Using db-to-sqlite with PostgreSQL schemasIf the tables you want to copy from your PostgreSQL database aren't in the default schema, you can specify an alternate one with the--postgres-schemaoption:db-to-sqlite "postgresql://localhost/myblog" blog.db \
    --all \
    --postgres-schema my_schemaUsing db-to-sqlite with MS SQLThe best way to get the connection string needed for the MS SQL connections below is to use urllib from the Standard Library as belowparams = urllib.parse.quote_plus(
    "DRIVER={SQL Server Native Client 11.0};"
    "SERVER=localhost;"
    "DATABASE=my_database;"
    "Trusted_Connection=yes;"
)The above will resolve toDRIVER%3D%7BSQL+Server+Native+Client+11.0%7D%3B+SERVER%3Dlocalhost%3B+DATABASE%3Dmy_database%3B+Trusted_Connection%3DyesYou can then use the string above in the odbc_connect belowmssql+pyodbc:///?odbc_connect=DRIVER%3D%7BSQL+Server+Native+Client+11.0%7D%3B+SERVER%3Dlocalhost%3B+DATABASE%3Dmy_database%3B+Trusted_Connection%3Dyes
mssql+pyodbc:///?odbc_connect=DRIVER%3D%7BSQL+Server+Native+Client+11.0%7D%3B+SERVER%3Dlocalhost%3B+DATABASE%3Dmy_database%3B+UID%3Dusername%3B+PWD%3DpasswdUsing db-to-sqlite with Heroku PostgresIf you run an application onHerokuusing theirPostgres database product, you can use theheroku configcommand to access a compatible connection string:$ heroku config --app myappname | grep HEROKU_POSTG
HEROKU_POSTGRESQL_OLIVE_URL: postgres://username:password@ec2-xxx-xxx-xxx-x.compute-1.amazonaws.com:5432/dbnameYou can pass this todb-to-sqliteto create a local SQLite database with the data from your Heroku instance.You can even do this using a bash one-liner:$ db-to-sqlite $(heroku config --app myappname | grep HEROKU_POSTG | cut -d: -f 2-) \
    /tmp/heroku.db --all -p
1/23: django_migrations
...
17/23: blog_blogmark
[####################################]  100%
...Related projectsDatasette: A tool for exploring and publishing data. Works great with SQLite files generated usingdb-to-sqlite.sqlite-utils: Python CLI utility and library for manipulating SQLite databases.csvs-to-sqlite: Convert CSV files into a SQLite database.DevelopmentTo set up this tool locally, first checkout the code. Then create a new virtual environment:cd db-to-sqlite
python3 -m venv venv
source venv/bin/activateOr if you are usingpipenv:pipenv shellNow install the dependencies and test dependencies:pip install -e '.[test]'To run the tests:pytestThis will skip tests against MySQL or PostgreSQL if you do not have their additional dependencies installed.You can install those extra dependencies like so:pip install -e '.[test_mysql,test_postgresql]'You can alternative usepip install psycopg2-binaryif you cannot install thepsycopg2dependency used by thetest_postgresqlextra.SeeRunning a MySQL server using Homebrewfor tips on running the tests against MySQL on macOS, including how to install themysqlclientdependency.The PostgreSQL and MySQL tests default to expecting to run against servers on localhost. You can use environment variables to point them at different test database servers:MYSQL_TEST_DB_CONNECTION- defaults tomysql://root@localhost/test_db_to_sqlitePOSTGRESQL_TEST_DB_CONNECTION- defaults topostgresql://localhost/test_db_to_sqliteThe database you indicate in the environment variable -test_db_to_sqliteby default - will be deleted and recreated on every test run.