Skip to content

Commit

Permalink
adds inline env var example
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Jan 2, 2023
1 parent f8c9d1b commit 647bea7
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions doc/connecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,35 @@ a flag with (-a|--connection_arguments)the connection string as a JSON string. S
%load_ext sql
```

### Building connection string

One option is to use `getpass`, type your password, build your connection string and pass it to `%sql`:

```{code-cell} ipython3
from getpass import getpass
```
+++

```python
from getpass import getpass

password = getpass()
connection_string = f"postgresql://user:{password}@localhost/database"
%sql $connection_string
```

+++

You may also set the `DATABASE_URL` environment variable, and `%sql` will automatically load it from there:
### Using `DATABASE_URL`

+++

You may also set the `DATABASE_URL` environment variable, and `%sql` will automatically load it from there. You can do it either by setting the environment variable from your terminal or in your notebook:

```python
from getpass import getpass
from os import environ

password = getpass()
environ["DATABASE_URL"] = f"postgresql://user:{password}@localhost/database"
```

```python
# without any args, %sql reads from DATABASE_URL
Expand Down

0 comments on commit 647bea7

Please sign in to comment.