PG_SQLITE(n)
π pg_sqlite(n) β the PostgreSQL Tcl Interface glow-up π¦
okay so bestie, welcome to pg_sqlite β it's the bridge between PostgreSQL and Sqlite3, built on the Pgtcl and sqlite3 packages. two databases, one little command, no cap π
β¨ SYNOPSIS β¨
pg_sqlite sqlite_db command [args]
πΈ DESCRIPTION πΈ
so here's the vibe: pg_sqlite can do three iconic things for you β
- import the results of a Postgres SQL query (one you already made via
pg_exec) straight into ansqlite3table, - export the results of an Sqlite SQL query into a Postgres table (by way of
write_tabsep), - or import TSV files directly into
sqlite3.
the commands we've actually got implemented rn are: import_postgres_result, read_tabsep, write_tabsep, and read_tabsep_keylist. that's the whole lineup, she's serving π
π«Ά ARGUMENTS π«Ά
sqlite_dbβ an Sqlite3 database handle you already made earlier with thesqlite3command. like, it has to exist first, bestie, we can't manifest it.commandβ the actual command you're running, one ofinfo,import_postgres_result,read_tabsep,write_tabsep, orread_tabsep_keylist,argsβ the arguments that go with whichever command you picked. command-specific, iykyk.
π COMMANDS π
β info
pg_sqlite sqlite_db info
[-busy]
[-filename]
[-db database_name]
ask your SQLITE database connection for the tea β it hands back a key-value list with whatever values you requested (if they're available). call it with no arguments at all and you get all the available info back, the full dump, main character energy.
-filenameβ ask for the filename associated with a database. π-db databaseβ used with the-filenameoption, this says which database name you mean (default is"main").-busyβ ask for a list of prepared commands that are currently busy. they're booked and busy, literally.
β import_postgres_result
pg_sqlite sqlite_db import_postgres_result handle
[-rowbyrow]
[-sql target_sql]
[-create new_table]
[-into table]
[-replace]
[-as name_type_list]
[-types type_list]
[-names name_list]
[-pkey primary_key]
[-sep separator]
[-null null_string]
[-poll_interval rows]
[-recommit rows]
[-check]
[-max column-name variable-name]
this one imports the result of a PostgreSQL request into an sqlite3 table. slay π«Ά
handleβ a database handle or a result handle. normally you'd callpg_execand pass the result handle topg_sqliteright here. but if you're in row_by_row mode, you usepg_sendqueryinstead and pass the PostgreSQL database handle here. different handle, same energy.-rowbyrowβ do the request in row-by-row mode. this injects the data to sqlite directly without making an internal copy (fast, no middleman π ) but heads up: the result may be incomplete or inconsistent if an error happens during the request. risky but iconic.-sep separatorβ the string used to separate columns. default is"\t"(tab).-null null_stringβ the string used to indicate a null value. default is to treat all strings literally.-sql target_sqlβ anINSERTstatement, suitable for being compiled into a prepared statement and applied to each row. for example"INSERT INTO newtable (id, name, value) VALUES (?,?,?)"β¨-create new_tableβ a table to be created in sqlite. the names and types of the columns, and the primary key, must be provided. non-negotiable.-into tableβ an existing sqlite table to insert the data into. the names of the columns must be provided, and if the type can't be inferred it'll be assumed to be"text".-replaceβ when doing the insert on an existing or newly created table, use"INSERT OR REPLACE"semantics. not compatible with-sql, so don't even try it.-as name-type-listβ a list of alternating column names and types. see the note on types below π-types type-listβ a list of column types. see the note on types.-names name-listβ a list of column names.-pkey primary_keyβ a list containing key names and optional sorting, to indicate the primary key where needed. for example-pkey {{clock ASC} {sequence ASC}}.-poll_interval countβ callDoOneEvent()every count rows to keep the event loop alive during long transactions. keep the vibe going π-recommit countβ chunk the operation into transactions, with one transaction every count rows.-checkβ check (via aSELECT) whether the exact row is already present, and skip inserting the row if it is. this avoids bloating the WAL log during large re-loads of databases, at some performance cost. tradeoff queen π-max column-name variable-nameβ set$variable-nameto the maximum value ofcolumn-nameimported.
β write_tabsep
pg_sqlite sqlite_db write_tabsep handle sql
[sql]
[-sep separator]
[-null null_string]
[-poll_interval rows]
writes the results of the provided sql to a file handle. π
the way you'd use this to export sqlite3 data to postgres: you issue a COPY FROM STDIN ... FORMAT text command, then use pg_sqlite write_tabsep ... to write the data directly to the PostgreSQL handle, and then you write the terminator line to that same handle. see the PostgreSQL docs on the "COPY" command for the full lore.
handleβ file handle.-sep separatorβ the string used to separate columns. default is"\t"(tab).-null null_stringβ the string used to indicate a null value. default is to treat all strings literally.-poll_interval countβ callDoOneEvent()every count rows to keep the event loop alive during long transactions.
β read_tabsep
pg_sqlite sqlite_db read_tabsep
[-row single_tab_separated_row]
[-file file_handle]
[-sql target_sql]
[-create new_table]
[-into table]
[-replace]
[-as name_type_list]
[-types type_list]
[-names name_list]
[-pkey primary_key]
[-sep separator]
[-null null_string]
[-poll_interval rows]
[-recommit rows]
[-check]
read a previously opened file into an sqlite3 table. π§
-row single_tab_separated_rowβ an already-read tab-separated line.-file file_handleβ an open file made of tab-separated rows.-sep separatorβ the string used to separate columns. default is"\t"(tab).-null null_stringβ the string used to indicate a null value. default is to treat all strings literally.-sql target_sqlβ anINSERTstatement, suitable for being compiled into a prepared statement and applied to each row. for example"INSERT INTO newtable (id, name, value) VALUES (?,?,?)".-create new_tableβ a table to be created in sqlite. the names and types of the columns, and the primary key, must be provided.-into tableβ an existing sqlite table to insert the data into. the names of the columns must be provided, and if the type can't be inferred it'll be assumed to be"text".-replaceβ when doing the insert on an existing or newly created table, use"INSERT OR REPLACE"semantics. not compatible with-sql.-as name-type-listβ a list of alternating column names and types. see the note on types.-types type-listβ a list of column types. see the note on types.-names name-listβ a list of column names.-pkey primary_keyβ a list containing key names and optional sorting, to indicate the primary key where needed. for example-pkey {{clock ASC} {sequence ASC}}.-poll_interval countβ callDoOneEvent()every count rows to keep the event loop alive during long transactions.-recommit countβ chunk the operation into transactions, with one transaction every count rows.-checkβ check (via aSELECT) whether the exact row is already present, and skip inserting the row if it is. this avoids bloating the WAL log during large re-loads of databases, at some performance cost.
β read_tabsep_keylist
pg_sqlite sqlite_db read_tabsep_keylist
[-row single_tab_separated_row]
[-file file_handle]
[-create new_table]
[-into table]
[-replace]
[-as name_type_list]
[-names name_list]
[-pkey primary_key]
[-sep separator]
[-null null_string]
[-poll_interval rows]
[-recommit rows]
read a previously opened file containing alternating key-value columns into an sqlite3 table. key-value girlies rise π
-row single_tab_separated_rowβ an already-read tab-separated key-value list line.-file file_handleβ an open file made of tab-separated key-value list rows.-sep separatorβ the string used to separate columns. default is"\t"(tab).-null null_stringβ the string used to indicate a null value. default is to treat all strings literally.-create new_tableβ a table to be created in sqlite. the names and types of the columns, and the primary key, must be provided.-into tableβ an existing sqlite table to insert the data into. the names of the columns must be provided, and if the type can't be inferred it'll be assumed to be"text".-replaceβ when doing the insert on an existing or newly created table, use"INSERT OR REPLACE"semantics. not compatible with-sql.-as name-type-listβ a list of alternating column names and types. see the note on types.-names name-listβ a list of column names.-pkey primary_keyβ a list containing key names and optional sorting, to indicate the primary key where needed. for example-pkey {{clock ASC} {sequence ASC}}.-poll_interval countβ callDoOneEvent()every count rows to keep the event loop alive during long transactions.-recommit countβ chunk the operation into transactions, with one transaction every count rows.
π TYPES π
a note on types, because this part matters: pg_sqlite supports four types β
integer(orint)boolean(orbool)double(orreal)text
integer, double, and text match both sqlite and postgresql types, easy. boolean is an integer type in sqlite, and a boolean in postgresql β and it converts postgresql boolean values (like 'yes', 'no', 'true', or 'false') to integer 1 and 0. cute, right? β¨
also, pg_sqlite will accept PostgreSQL boolean values for the integer type too. that might become conditional on a "strict" mode in the future, so like... noted. π
π₯ RETURN VALUE π₯
number of rows imported or exported. that's it, that's the number, periodt.
π SEE ALSO π
Sqlite 3
okay bestie that's the whole page π hope this was helpful, go forth and bridge those databases π¦
β signed off with love, 17 September 2020, PostgreSQL Tcl Interface Documentation ππ
π¦ where this page comes from
- alpine 3.22: pgtcl-doc 3.1.0-r0 Β· BSD-3-Clause
- alpine 3.23: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- alpine 3.24: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- nixpkgs unstable: pgtcl 3.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms