databasePostgres

To connect to a Postgres database, we need to make sure the environment has the appropriate modules installed, such as connectorx which serves as the engine.

1

Define dependency

Add the dependency to _config.json:

{
  "packages": [
    "connectorx==0.4.4"
  ]
}
2

Specify connection configuration and query

Provide the connection details (user, password, host, port, database name) and run a SELECT query (you can filter with a WHERE clause if needed):

import polars as pl

db_user = 'reader'
db_password = 'NWDMCE5xdipIjRrp'
db_host = 'hh-pgsql-public.ebi.ac.uk'
db_port = 5432
db_name = 'pfmegrnargs'

conn_uri = f"postgres://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"

def transform():
    return pl.read_database_uri(
        query="SELECT * FROM Rna LIMIT 400",
        uri=conn_uri,
        engine="connectorx"
    )

For more information, visit the Polars documentation page about read_database_uri().arrow-up-right