Setting Up PostgreSQL for Full Stack Python Projects

Setting Up PostgreSQL for Full Stack Python Projects

What is PostgreSQL?

PostgreSQL is a powerful, open-source relational database management system. It's known for being reliable, feature-rich, and widely used in modern full stack applications.


In a full stack Python project, PostgreSQL is often used to store and manage application data (like user accounts, posts, or transactions), and it integrates well with frameworks like Django and Flask.


Step-by-Step Guide to Set Up PostgreSQL

Step 1: Install PostgreSQL

Windows

Download the installer from https://www.postgresql.org/download/windows/


Run the installer and follow the steps.


During installation, set:


A password for the postgres user (remember it!)


Default port: 5432


macOS

Use Homebrew:


bash

Copy

Edit

brew install postgresql

brew services start postgresql

Linux (Ubuntu/Debian)

bash

Copy

Edit

sudo apt update

sudo apt install postgresql postgresql-contrib

Step 2: Access PostgreSQL

Use the built-in CLI tool psql:


bash

Copy

Edit

psql -U postgres

If prompted, enter the password you set during installation.


To exit the psql console, type:


sql

Copy

Edit

\q

Step 3: Create a Database and User

After logging into psql, run:


sql

Copy

Edit

CREATE DATABASE myprojectdb;

CREATE USER myprojectuser WITH PASSWORD 'mypassword';

ALTER ROLE myprojectuser SET client_encoding TO 'utf8';

ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';

ALTER ROLE myprojectuser SET timezone TO 'UTC';

GRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;

Replace myprojectdb, myprojectuser, and 'mypassword' with your own names.


Step 4: Connect PostgreSQL to Python

You’ll need a database driver like psycopg2 or SQLAlchemy.


Install psycopg2

bash

Copy

Edit

pip install psycopg2-binary

Example with SQLAlchemy

python

Copy

Edit

from sqlalchemy import create_engine


engine = create_engine("postgresql://myprojectuser:mypassword@localhost/myprojectdb")


# Test connection

with engine.connect() as conn:

    result = conn.execute("SELECT version();")

    for row in result:

        print(row)

Step 5: Use PostgreSQL with a Web Framework

Django

In settings.py:


python

Copy

Edit

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.postgresql',

        'NAME': 'myprojectdb',

        'USER': 'myprojectuser',

        'PASSWORD': 'mypassword',

        'HOST': 'localhost',

        'PORT': '5432',

    }

}

Run:


bash

Copy

Edit

python manage.py migrate

Flask with SQLAlchemy

python

Copy

Edit

from flask import Flask

from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://myprojectuser:mypassword@localhost/myprojectdb'

db = SQLAlchemy(app)

Step 6: (Optional) Use a GUI Tool

You can use tools like:


pgAdmin (official GUI tool for PostgreSQL)


DBeaver or TablePlus (more general SQL tools)


They make it easier to manage databases, run queries, and inspect data visually.


Summary

To set up PostgreSQL for your Python full stack project:


Install PostgreSQL.


Create a database and user.


Connect using Python with psycopg2 or SQLAlchemy.


Configure your web framework (like Flask or Django).


Optionally, use a GUI tool for easier management.

Learn Full Stack Python Course in Hyderabad

Read More

SQL vs NoSQL: What’s Best for Full Stack Python Development?

Introduction to Databases for Full Stack Python Development

What is Full Stack Development and Why Python?

Databases and Data Storage in python

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing

A Beginner's Guide to ETL Testing: What You Need to Know