Postgres
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.
We can define the dependency inside the _config.json
{
"packages": [
"connectorx==0.4.4"
]
}
Once installed, the connection configuration can be specified, including the user, password, host, and name of the database, and directly run a SELECT *
SQL query against the database to extract everything or apply some filtering in the WHERE
clause.
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().
Last updated