Skip to content

Commit 8e3b2dd

Browse files
authored
Add psycopg example on Direct Database Connections (#174)
1 parent 16a93cf commit 8e3b2dd

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

docs/guides/advanced/direct_database_connections.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ To connect, you'll first need to generate generate database credentials from you
1212

1313
The credentials generated are a PostgreSQL URI which can be used as a connection string for compatible tools. These will only be shown by the Logfire platform once, so save them to a secure location for future use!
1414

15-
## Example: pgcli
15+
## Connecting to the database
16+
17+
As mentioned, you can use the generated credentials to connect to the database using any tool that supports PostgreSQL connections.
18+
19+
### Using `pgcli`
1620

1721
[`pgcli`](https://www.pgcli.com/) is a command-line tool to access PostgreSQL databases.
1822

1923
Using the credentials generated in the previous step as the argument to `pgcli`, you can connect directly to **Logfire**:
2024

21-
```
25+
```bash
2226
$ pgcli postgresql://<user>:<password>@db.logfire.dev:5432/proj_david-test # REDACTED
2327
Version: 4.0.1
2428
Home: http://pgcli.com
@@ -31,7 +35,7 @@ proj_david-test> select start_timestamp, message from records limit 10;
3135
| 2024-04-28 10:50:41.665576+00 | GET /contact/ |
3236
| 2024-04-28 10:50:41.711119+00 | GET /contact/ http send response.start |
3337
| 2024-04-28 10:50:41.709458+00 | response 500 |
34-
| 2024-04-28 10:50:38.50534+00 | action=view-cart size=517 i=0 |
38+
| 2024-04-28 10:50:38.505343+00 | action=view-cart size=517 i=0 |
3539
| 2024-04-28 10:50:39.446668+00 | action=view-faq size=637 i=2 |
3640
| 2024-04-28 10:50:38.681198+00 | action=view-terms size=216 i=3 |
3741
| 2024-04-28 10:50:39.416706+00 | action=view-product size=380 i=0 |
@@ -41,4 +45,40 @@ SELECT 10
4145
Time: 0.218s
4246
```
4347
44-
With the flexibility of PostgreSQL access available to you, we can't wait to hear what you do with the Logfire platform!
48+
### Using `psycopg`
49+
50+
[`psycopg`](https://www.psycopg.org/) is a Python library that allows you to connect to PostgreSQL databases.
51+
52+
You can use the credentials generated in the previous step to connect to **Logfire**:
53+
54+
```python title='main.py'
55+
import os
56+
57+
import psycopg
58+
59+
POSTGRES_URI = os.getenv('POSTGRES_URI')
60+
61+
with psycopg.connect(POSTGRES_URI) as conn:
62+
cursor = conn.cursor()
63+
cursor.execute('SELECT start_timestamp, message FROM records LIMIT 10')
64+
for start_timestamp, message in cursor.fetchall():
65+
print(f'{start_timestamp}: {message}')
66+
```
67+
68+
Set the `export POSTGRES_URI=...` environment variable to the URI generated in the previous step, and run the
69+
script with `python main.py`:
70+
71+
```bash
72+
2024-04-28 10:50:41.681886+00: action=view-faq size=549 i=0
73+
2024-04-28 10:50:41.711747+00: GET /contact/ http send response.body
74+
2024-04-28 10:50:41.665576+00: GET /contact/
75+
2024-04-28 10:50:41.711119+00: GET /contact/ http send response.start
76+
2024-04-28 10:50:41.709458+00: response 500
77+
2024-04-28 10:50:38.505343+00: action=view-cart size=517 i=0
78+
2024-04-28 10:50:39.446668+00: action=view-faq size=637 i=2
79+
2024-04-28 10:50:38.681198+00: action=view-terms size=216 i=3
80+
2024-04-28 10:50:39.416706+00: action=view-product size=380 i=0
81+
2024-04-28 10:50:38.394237+00: sub-sub-sub-action=logout
82+
```
83+
84+
With the flexibility of PostgreSQL access available to you, we can't wait to hear what you do with **Logfire**! :fire:

0 commit comments

Comments
 (0)