<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc" -->

---
title: 12 Days of DigitalOcean - Setting Up a PostgreSQL...
description: Learn how to set up a PostgreSQL database on DigitalOcean for storing birthday reminders and how to securely connect to it using Python. This guide covers...
canonical: https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: 12 Days of DigitalOcean - Setting Up a PostgreSQL Database for Birthday Reminders | daily.dev
og:description: Learn how to set up a PostgreSQL database on DigitalOcean for storing birthday reminders and how to securely connect to it using Python. This guide covers...
og:url: https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc
og:image: https://api.daily.dev/og/posts/4zuewkFhC.png
og:image:alt: 12 Days of DigitalOcean - Setting Up a PostgreSQL Database for Birthday Reminders
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 12 Days of DigitalOcean - Setting Up a PostgreSQL Database for Birthday Reminders

**[Collections](https://daily.dev/sources/collections)** · 2 min read · 2 upvotes · 0 comments

## Summary

Learn how to set up a PostgreSQL database on DigitalOcean for storing birthday reminders and how to securely connect to it using Python. This guide covers database creation, table setup, inserting sample data, securing the database, and writing a Python script using `pg8000` and `python-dotenv` for secure connection and data querying.

## Content

# 12 Days of DigitalOcean: Setting Up and Connecting a PostgreSQL Database for Birthday Reminders with Python

Welcome to Day 2 of DigitalOcean's 12-day series! Today, we'll dive into setting up a PostgreSQL database designed for storing birthday reminders and securely connecting to it using Python.

## Setting Up the PostgreSQL Database

First, you'll want to set up your PostgreSQL database on DigitalOcean. Follow these steps:

1. **Create the Database**:
   
   Use the `psql` command-line tool to create your database:
   
   ```sh
   createdb birthday_reminders
   ```

2. **Set Up the Contacts Table**:
   
   Next, create a 'contacts' table to store essential information:
   
   ```sql
   CREATE TABLE contacts (
       id SERIAL PRIMARY KEY,
       name VARCHAR(100),
       birthdate DATE
   );
   ```

3. **Insert Sample Data**:
   
   Populate the table with some initial data:
   
   ```sql
   INSERT INTO contacts (name, birthdate) VALUES ('John Doe', '1990-01-15');
   INSERT INTO contacts (name, birthdate) VALUES ('Jane Smith', '1985-05-23');
   ```

4. **Secure Your Database**:
   
   Ensure your database is secure by configuring access from trusted sources only.

## Connecting to the PostgreSQL Database with Python

Now, let’s write a Python script to connect to your PostgreSQL database.

### Prerequisites

- Python installed on your machine
- Libraries: `pg8000` and `python-dotenv`

### Steps

1. **Install Required Libraries**:
   
   ```sh
   pip install pg8000 python-dotenv
   ```

2. **Set Up the `.env` File**:
   
   Create a `.env` file in your project directory to store your database credentials securely:
   
   ```env
   DB_HOST=your_database_host
   DB_NAME=birthday_reminders
   DB_USER=your_database_user
   DB_PASS=your_database_password
   ```

3. **Write the Python Script**:
   
   Create a Python script to connect and query the database, ensuring your credentials are not hard-coded:
   
   ```python
   import os
   import pg8000
   from dotenv import load_dotenv

   # Load environment variables
   load_dotenv()

   DB_HOST = os.getenv('DB_HOST')
   DB_NAME = os.getenv('DB_NAME')
   DB_USER = os.getenv('DB_USER')
   DB_PASS = os.getenv('DB_PASS')

   # Connect to the database
   conn = pg8000.connect(user=DB_USER, password=DB_PASS, host=DB_HOST, database=DB_NAME)
   cursor = conn.cursor()

   # Query the contacts table
   cursor.execute("SELECT * FROM contacts;")
   for row in cursor.fetchall():
       print(row)

   cursor.close()
   conn.close()
   ```

By following these steps, you ensure that your Python script connects securely to the PostgreSQL database. This makes your code ready for deployment without exposing sensitive credentials. Stay tuned for the next part of the series where we'll extend this setup to send SMS reminders for birthdays.

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 1 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments
- [CNCF Unveils Schedule for KubeCon \+ CloudNativeCon Europe 2026](https://daily.dev/posts/cncf-unveils-schedule-for-kubecon-cloudnativecon-europe-2026-ikhcoa5cb) · CNCF · 2 upvotes · 0 comments
- [CNCF Debuts KubeCon \+ CloudNativeCon Japan 2026 Schedule](https://daily.dev/posts/cncf-debuts-kubecon-cloudnativecon-japan-2026-schedule-xp5pyudub) · CNCF · 1 upvotes · 0 comments

---

Tags: [#database](https://daily.dev/tags/database), [#postgresql](https://daily.dev/tags/postgresql), [#digitalocean](https://daily.dev/tags/digitalocean)

[View this post on daily.dev](https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"12 Days of DigitalOcean - Setting Up a PostgreSQL Database for Birthday Reminders","url":"https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc"},"datePublished":"2024-12-17T14:30:54.445Z","dateModified":"2024-12-18T20:00:36.533Z","description":"Learn how to set up a PostgreSQL database on DigitalOcean for storing birthday reminders and how to securely connect to it using Python. This guide covers...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/f8c621694aef2795410a288946f911e1?_a=AQAEuj9","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/f8c621694aef2795410a288946f911e1?_a=AQAEuj9","isAccessibleForFree":true,"articleSection":"Collections","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Collections","logo":"https://media.daily.dev/image/upload/s--fk_6ycEi--/f_auto,q_auto/v1780996001/logos/collections?_a=BAMAMiWQ0","url":"https://daily.dev/sources/collections"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/12-days-of-digitalocean---setting-up-a-postgresql-database-for-birthday-reminders-4zuewkfhc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"database,postgresql,digitalocean","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Collections","item":"https://daily.dev/sources/collections"},{"@type":"ListItem","position":3,"name":"12 Days of DigitalOcean - Setting Up a PostgreSQL Database for Birthday Reminders"}]}
```

