Featured image of post Building a Python CLI Password Manager with Click and SQLite

Building a Python CLI Password Manager with Click and SQLite

Because command line apps that use sql are fun :)

Building a CLI Password Manager with Click and SQLite

Letโ€™s be realโ€”password management is a nightmare. You either:

  1. Use the same weak password for everything (bad idea).
  2. Store them in a Notepad file called “passwords.txt” (even worse).
  3. Forget them and reset them every time (annoying).

But what if you could build your own password manager in Python? One that runs from the command line and securely stores your passwords in a SQLite database?

Thatโ€™s what weโ€™re doing today! Weโ€™ll use Click (for a clean CLI interface) and SQLite (for safe storage) to build a simple but functional password manager.

By the end of this, youโ€™ll have:

  • A CLI tool to save and retrieve passwords securely.
  • A simple database that stores credentials.
  • A cool hacking movie feel when you type commands.

๐Ÿ›  What Youโ€™ll Need

First, install Click and SQLite3 (though SQLite is built into Python):

1
pip install click

๐Ÿ”‘ Step 1: Setting Up SQLite

We need a database to store our passwords. SQLite is perfect for this because:

  • Itโ€™s lightweight and built into Python (no setup needed).
  • Itโ€™s secure (as long as nobody gets access to the DB file).
  • Itโ€™s easy to use (because who wants complex SQL?).

Letโ€™s create a simple database handler in a file called db.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sqlite3

DB_FILE = "passwords.db"

def init_db():
    """Creates the passwords table if it doesn't exist."""
    with sqlite3.connect(DB_FILE) as conn:
        cursor = conn.cursor()
        cursor.execute("""
            CREATE TABLE IF NOT EXISTS passwords (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                service TEXT NOT NULL,
                username TEXT NOT NULL,
                password TEXT NOT NULL
            )
        """)
        conn.commit()

def add_password(service, username, password):
    """Stores a new password in the database."""
    with sqlite3.connect(DB_FILE) as conn:
        cursor = conn.cursor()
        cursor.execute("INSERT INTO passwords (service, username, password) VALUES (?, ?, ?)", 
                       (service, username, password))
        conn.commit()

def get_password(service):
    """Retrieves a password from the database."""
    with sqlite3.connect(DB_FILE) as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT username, password FROM passwords WHERE service = ?", (service,))
        result = cursor.fetchone()
        return result

Now, we have:
โœ… A database
โœ… A function to add passwords
โœ… A function to retrieve passwords

Letโ€™s move to the CLI part.


๐ŸŽฎ Step 2: Building the CLI with Click

Now, letโ€™s create password_manager.py and hook it up with Click.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import click
import db

# Initialize the database
db.init_db()

@click.group()
def cli():
    """Simple CLI Password Manager"""
    pass

@click.command()
@click.argument("service")
@click.argument("username")
@click.argument("password")
def add(service, username, password):
    """Adds a new password entry."""
    db.add_password(service, username, password)
    click.secho(f"โœ… Password for {service} saved!", fg="green")

@click.command()
@click.argument("service")
def get(service):
    """Retrieves a stored password."""
    result = db.get_password(service)
    if result:
        username, password = result
        click.secho(f"๐Ÿ”‘ Service: {service}", fg="cyan")
        click.secho(f"๐Ÿ‘ค Username: {username}", fg="yellow")
        click.secho(f"๐Ÿ”’ Password: {password}", fg="red")
    else:
        click.secho("โŒ No password found!", fg="red")

# Add commands to the CLI group
cli.add_command(add)
cli.add_command(get)

if __name__ == "__main__":
    cli()

How it Works:

  • add <service> <username> <password> โ†’ Stores a password.
  • get <service> โ†’ Retrieves a stored password.
  • Colors make it easier to read!

๐Ÿƒโ€โ™‚๏ธ Step 3: Running Your Password Manager

1. Add a New Password

1
python password_manager.py add twitter myuser supersecure123
1
โœ… Password for twitter saved!

2. Retrieve a Password

1
python password_manager.py get twitter
1
2
3
๐Ÿ”‘ Service: twitter
๐Ÿ‘ค Username: myuser
๐Ÿ”’ Password: supersecure123

โœจ Boom! You now have a fully functional password manager in Python.


๐Ÿ” Step 4: Improving Security (Optional)

Right now, our passwords are stored in plain textโ€”which is bad.

1. Hashing Passwords

Instead of storing raw passwords, letโ€™s hash them using bcrypt:

1
pip install bcrypt

Modify db.py to hash passwords before saving:

1
2
3
4
5
6
7
8
9
import bcrypt

def hash_password(password):
    """Hashes a password before storing it."""
    return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()

def verify_password(stored_hash, password):
    """Checks if a password matches the stored hash."""
    return bcrypt.checkpw(password.encode(), stored_hash.encode())

Then, change add_password() to store the hash instead of plain text.

Now, even if someone steals your database, they wonโ€™t be able to read the passwords!


๐Ÿš€ Bonus: Making It Even Cooler

  • ๐Ÿ”‘ Auto-generate strong passwords using secrets
  • ๐Ÿ“ฆ Add categories (e.g., work, personal, banking)
  • ๐Ÿ“ Store passwords in an encrypted file instead of SQLite
  • ๐Ÿ” Require a master password to access stored passwords

If youโ€™re feeling fancy, turn this into a full GUI app later using Tkinter or Rich.


๐ŸŽ‰ Wrapping Up

Today, you learned how to build your own CLI password manager using:
โœ… Click for an easy-to-use command-line interface
โœ… SQLite for lightweight, persistent storage
โœ… bcrypt for password hashing (security matters!)

Now, you never have to forget your passwords againโ€”unless you forget the command to retrieve them. ๐Ÿ˜…


๐Ÿ“š References

  1. Click Official Docs
  2. SQLite Python Documentation
  3. bcrypt Python Library
  4. Python Secrets Module