# 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.

{% stepper %}
{% step %}

### Define dependency

Add the dependency to `_config.json`:

{% code title="\_config.json" %}

```json
{
  "packages": [
    "connectorx==0.4.4"
  ]
}
```

{% endcode %}
{% endstep %}

{% step %}

### 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):

```python
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"
    )
```

{% endstep %}
{% endstepper %}

For more information, visit the Polars documentation page about [read\_database\_uri().](https://docs.pola.rs/api/python/stable/reference/api/polars.read_database_uri.html)
