This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Send and receive data from PostgreSQL, SQLite, MySQL, TigerData, or
Snowflake for use in external applications.
What’s in this recipe:
- Export entire tables or filtered queries to any SQL database
- Importing exported data back into Pixeltable
- Select specific columns for export
- Handle existing tables with replace or append options
- Connect to cloud PostgreSQL services (e.g. TigerData)
Problem
You have processed data in your pipeline—cleaned text, generated
embeddings, extracted metadata—and need to send it to a SQL database for
use by other applications or teams.
Solution
You use export_sql() & import_sql() to transfer tables or queries to
and from any SQL database. The functions automatically map Pixeltable
types to appropriate SQL types for each database dialect.
Setup
Create sample data
Created directory ‘sql_export_demo’.
<pixeltable.catalog.dir.Dir at 0x17fec0790>
Created table ‘products’.
Inserted 5 rows with 0 errors in 0.01 s (501.18 rows/s)
5 rows inserted.
Export an entire table
You pass a table and a SQLAlchemy connection string to export all rows
and columns.
[(‘Wireless Mouse’, 29.99, 1, ’{“rating”: 4.5, “category”: “electronics”}’),
(‘USB-C Hub’, 49.99, 0, ’{“rating”: 4.2, “category”: “electronics”}’),
(‘Mechanical Keyboard’, 89.99, 1, ’{“rating”: 4.8, “category”: “electronics”}’),
(‘Monitor Stand’, 39.99, 1, ’{“rating”: 4.0, “category”: “accessories”}’),
(‘Webcam’, 59.99, 0, ’{“rating”: 3.9, “category”: “electronics”}’)]
Export a filtered query
You can export any query result—filter rows, select specific columns, or
apply transformations before export.
[(‘Wireless Mouse’, 29.99),
(‘Mechanical Keyboard’, 89.99),
(‘Monitor Stand’, 39.99)]
Import a table & query
You can use import_sql() to import an existing SQL table or query into
Pixeltable. pxt.Table schema is automatically inferred from the schema
of the existing SQL table, and can be overridden using
schema_overrides.
Created table ‘imported_products’.
Inserted 5 rows with 0 errors in 0.01 s (779.78 rows/s)
Created table ‘imported_products_lossy’.
Inserted 5 rows with 0 errors in 0.01 s (867.33 rows/s)
Export specific columns
You select only the columns you need before exporting. You can also
rename columns in the output.
[‘name’, ‘price’]
Handle existing tables
You control what happens when the target table already exists using the
if_exists parameter:
‘Total rows after insert: 7’
“Columns: [‘name’, ‘price’], Row count: 5”
Export to cloud PostgreSQL (TigerData)
You can export directly to cloud-hosted PostgreSQL databases like
TigerData (Timescale Cloud). Get your
credentials from the TigerData dashboard after creating a service.
[(‘Wireless Mouse’, 29.99, True, {‘rating’: 4.5, ‘category’: ‘electronics’}),
(‘USB-C Hub’, 49.99, False, {‘rating’: 4.2, ‘category’: ‘electronics’}),
(‘Mechanical Keyboard’, 89.99, True, {‘rating’: 4.8, ‘category’: ‘electronics’}),
(‘Monitor Stand’, 39.99, True, {‘rating’: 4.0, ‘category’: ‘accessories’}),
(‘Webcam’, 59.99, False, {‘rating’: 3.9, ‘category’: ‘electronics’})]
Export to Snowflake
You can export directly to Snowflake data
warehouses. Get your account identifier from the Snowflake web interface
under Admin → Accounts.
[(‘Wireless Mouse’, 29.99, True, None),
(‘USB-C Hub’, 49.99, False, None),
(‘Mechanical Keyboard’, 89.99, True, None),
(‘Monitor Stand’, 39.99, True, None),
(‘Webcam’, 59.99, False, None)]
For tables containing media types (pxt.Image, pxt.Video,
pxt.Audio), you have two options:
-
Extract metadata before export - Select only the columns you
need (paths, embeddings, extracted text, etc.) and export those to
SQL.
-
Use Pixeltable destinations - For syncing media files to cloud
storage, use Pixeltable’s built-in destination support with
providers like
Tigris.
Example: Export image metadata to SQL
Created table ‘images’.
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.01 s
No rows affected.
Inserted 2 rows with 0 errors in 0.08 s (26.28 rows/s)
2 rows inserted.
[(‘cat’, 481, 640, ‘RGB’), (‘scene’, 640, 429, ‘RGB’)]
Media columns can’t be written to SQL directly, but their location can.
Select images.image.fileurl to export each image’s URL (here, the
original GitHub links) as a plain string column.
[(‘cat’, ‘https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000036.jpg'),
(‘scene’, ‘https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/resources/images/000000000090.jpg')\]
Import the locations back as images
Reflect the SQL table and import it, using schema_overrides to promote
the location column back to pxt.Image. Pixeltable rehydrates each URL
into a real image, so expressions like .width work again.
Created table ‘restored_images’.
Inserted 2 rows with 0 errors in 0.01 s (142.39 rows/s)
Explanation
Connection strings:
The function uses SQLAlchemy connection strings. Common formats:
Type mapping:
Pixeltable types map to SQL types automatically:
Unsupported types:
Media types like pxt.Image, pxt.Video, and pxt.Audio cannot be
exported directly. Extract the data you need (paths, embeddings,
metadata) before export.
See also