DEV Community

Cover image for # Setting Up PostgreSQL Locally and Connecting It to My Project (Beginner Journey)
Bharath Kumar_30
Bharath Kumar_30

Posted on

# Setting Up PostgreSQL Locally and Connecting It to My Project (Beginner Journey)

Hey everyone,

Today I worked on something very important in backend development — setting up a database and connecting it to my project.

Until now, I was mainly focused on coding features. But today I understood that without a proper database setup, a project is not complete.

So I decided to set up PostgreSQL locally and integrate it step by step.


What I focused on today

My main goal was to:

  • Set up PostgreSQL locally
  • Create a database
  • Configure environment variables
  • Connect database using Python
  • Execute my database schema

Step 1: Creating the database

After installing PostgreSQL, I created a database for my project:

```sql id="y2y3g1"
CREATE DATABASE social_media_db;




This is where all my project data will be stored.

---

##  Step 2: Managing credentials using .env

Instead of writing database credentials directly in code, I used a `.env` file.



```env id="3hkgmq"
DB_HOST=localhost
DB_PORT=5432
DB_NAME=social_media_db
DB_USER=postgres
DB_PASSWORD=your_password
Enter fullscreen mode Exit fullscreen mode

This made my setup cleaner and more secure.


Step 3: Connecting PostgreSQL with Python

To connect my project with the database, I used psycopg2.

```python id="s6x3jm"
import os
import psycopg2
from dotenv import load_dotenv

load_dotenv()

conn = psycopg2.connect(
host=os.getenv("DB_HOST"),
port=os.getenv("DB_PORT"),
dbname=os.getenv("DB_NAME"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASSWORD")
)

print("Database connected successfully!")




Seeing this connection work was a great moment.

---

##  Step 4: Executing my database design

Earlier, I had designed my database structure using DDL.

Today, I executed that design in PostgreSQL.

My schema includes:

* Users table
* Platforms table
* Credentials table
* Posts table
* Post logs table

Now my design is no longer just an idea — it’s a working database.

---

##  Step 5: Updating from SQLite to PostgreSQL

My project was previously using SQLite.

So I started adapting it to PostgreSQL.

I learned small but important differences:

* `AUTOINCREMENT` → `SERIAL`
* `DATETIME` → `TIMESTAMP`

These changes helped me understand how different databases work.

---

##  What I learned today

Today was not about writing a lot of code.

It was about understanding how things actually work behind the scenes.

I learned that:

* Database setup is a core part of backend development
* Environment variables are important for security
* Designing and executing a schema are two different steps
* Migrating between databases requires attention to detail

---

##  My takeaway

I feel more confident now working with databases.

Instead of just using default setups, I now understand how to:

* Create and manage databases
* Connect them to applications
* Structure data properly

---

##  Final thought

Every day I’m learning something new.

Some days it’s coding,
Some days it’s understanding systems.

Both are equally important.

This is just one more step in my journey — more learning ahead 🚀
Enter fullscreen mode Exit fullscreen mode

Top comments (0)