#!/usr/bin/env python3 """ Password URL encoder for PostgreSQL connection strings """ import urllib.parse import sys def encode_password(password: str) -> str: """URL encode a password for use in connection strings""" return urllib.parse.quote(password, safe='') def build_connection_string(username: str, password: str, host: str, port: int, database: str) -> str: """Build a properly encoded PostgreSQL connection string""" encoded_password = encode_password(password) return f"postgresql://{username}:{encoded_password}@{host}:{port}/{database}" def main(): print("PostgreSQL Password URL Encoder") print("=" * 40) if len(sys.argv) > 1: # Password provided as argument password = sys.argv[1] else: # Interactive mode password = input("Enter your password: ") encoded = encode_password(password) print(f"\nOriginal password: {password}") print(f"URL encoded: {encoded}") # Optional: build full connection string try: build_full = input("\nBuild full connection string? (y/N): ").strip().lower() == 'y' except (EOFError, KeyboardInterrupt): print("\nDone!") return if build_full: username = input("Username: ").strip() host = input("Host: ").strip() port = input("Port [5432]: ").strip() or "5432" database = input("Database [water_monitoring]: ").strip() or "water_monitoring" connection_string = build_connection_string(username, password, host, int(port), database) print(f"\nComplete connection string:") print(f"POSTGRES_CONNECTION_STRING={connection_string}") print(f"\nAdd this to your .env file:") print(f"DB_TYPE=postgresql") print(f"POSTGRES_CONNECTION_STRING={connection_string}") if __name__ == "__main__": main()