Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf47ea8

Browse files
added Postgresql example deepnote
1 parent b7c4907 commit cf47ea8

File tree

4 files changed

+837
-0
lines changed

4 files changed

+837
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
coverage
1010
dist
1111
node_modules
12+
.ipynb_checkpoints
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# PostgreSQL Integration Example
2+
3+
This example demonstrates how to connect to and work with PostgreSQL databases in Deepnote using Python and `psycopg2`.
4+
5+
## What You'll Learn
6+
7+
- Connecting to PostgreSQL from Python
8+
- Creating and managing database tables
9+
- Running CRUD operations (Create, Read, Update, Delete)
10+
- Querying data with pandas DataFrames
11+
- Filtering and aggregating data
12+
- Error handling and transactions
13+
14+
## Prerequisites
15+
16+
### Option 1: Quick Start with Docker (Recommended)
17+
18+
If you have Docker installed, start a PostgreSQL container with one command:
19+
20+
```bash
21+
docker run --name deepnote-postgres \
22+
-e POSTGRES_PASSWORD=postgres \
23+
-p 5432:5432 \
24+
-d postgres:15
25+
```
26+
27+
**Connection details:**
28+
- Host: `localhost`
29+
- Port: `5432`
30+
- Database: `postgres`
31+
- User: `postgres`
32+
- Password: `postgres`
33+
34+
**Don't have Docker?** Install it from [docker.com](https://www.docker.com/get-started)
35+
36+
### Option 2: Use Existing PostgreSQL
37+
38+
You can connect to any existing PostgreSQL instance (local, cloud, or remote) by updating the connection parameters in the notebook.
39+
40+
## Setup Instructions
41+
42+
1. **Start PostgreSQL** (see options above)
43+
44+
2. **Install Python dependencies:**
45+
```bash
46+
pip install psycopg2-binary pandas
47+
```
48+
49+
3. **Open the notebook:**
50+
- Open `postgresql_example.deepnote` in VS Code with the Deepnote extension
51+
- Or open it in Cursor, Windsurf, or JupyterLab
52+
53+
4. **Update connection details:**
54+
- Edit the second cell with your PostgreSQL credentials
55+
- Default credentials work with the Docker quick start
56+
57+
5. **Run the cells:**
58+
- Execute cells sequentially using `Cmd+Enter` (Mac) or `Ctrl+Enter` (Windows)
59+
- The notebook creates a sample `users` table for demonstration
60+
61+
## What's in This Example
62+
63+
The notebook covers:
64+
65+
1. **Connection Setup** - Configure and test PostgreSQL connection
66+
2. **Table Creation** - Create a `users` table with sample data
67+
3. **Data Querying** - Fetch and display data using pandas
68+
4. **Filtering** - Query specific records based on criteria
69+
5. **Aggregation** - Calculate statistics (COUNT, AVG, MIN, MAX)
70+
6. **Insert Operation** - Add new records to the database
71+
7. **Update Operation** - Modify existing records
72+
8. **Cleanup** - Drop the sample table when done
73+
74+
## Advanced Example
75+
76+
For a more complex scenario with multiple tables and relationships, see `sample_data.sql` which includes:
77+
- Customers, products, orders, and order_items tables
78+
- Foreign key relationships
79+
- Sample e-commerce data
80+
- Views for complex queries
81+
82+
Load it into your database:
83+
```bash
84+
# Docker
85+
docker exec -i deepnote-postgres psql -U postgres < sample_data.sql
86+
87+
# Local PostgreSQL
88+
psql -U postgres < sample_data.sql
89+
```
90+
91+
## Cleanup
92+
93+
To stop and remove the Docker container:
94+
95+
```bash
96+
docker stop deepnote-postgres
97+
docker rm deepnote-postgres
98+
```
99+
100+
## Troubleshooting
101+
102+
**`ModuleNotFoundError: No module named 'psycopg2'`**
103+
→ Install the package: `pip install psycopg2-binary`
104+
105+
**Connection refused or timeout**
106+
→ Check if PostgreSQL is running: `docker ps` or `brew services list`
107+
108+
**Authentication failed**
109+
→ Verify your credentials match the database configuration
110+
111+
**Port already in use**
112+
→ Change the Docker port mapping: `-p 5433:5432` (use 5433 instead of 5432)

0 commit comments

Comments
 (0)