Database Credentials

Database credentials - like hostnames, usernames, passwords, and ports - are the information needed to authenticate and connect to a database. Handling them securely is critical, since exposing credentials can lead to serious data breaches.

Typical Credential Fields

  • Host - the server address where the database runs
  • Port - the network port the database listens on
  • Database name - which specific database to connect to
  • Username / Password - credentials used to authenticate

What NOT to Do

# Avoid hardcoding credentials directly in your code
conn = psycopg2.connect(
    host="myserver.com",
    user="admin",
    password="SuperSecret123"   # never commit this!
)

A Safer Approach - Environment Variables

import os
import psycopg2

conn = psycopg2.connect(
    host=os.environ["DB_HOST"],
    user=os.environ["DB_USER"],
    password=os.environ["DB_PASSWORD"],
    dbname=os.environ["DB_NAME"]
)

Credentials are kept outside the code (in environment variables or a .env file that's excluded from version control), so they're never accidentally exposed in a shared repository.

Committing credentials to a public GitHub repository is one of the most common causes of real-world data breaches - always double-check what you're committing before pushing code.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course