# PostgreSQL User Management This guide covers creating and managing PostgreSQL users on the postgres server (192.168.200.103). ## Quick Reference ### Create Superuser with Random Password ```bash ansible-playbook playbooks/create-postgres-user.yml -e "pg_username=hiveops pg_superuser=true" ``` ### Create User with Specific Password ```bash ansible-playbook playbooks/create-postgres-user.yml -e "pg_username=myapp pg_password=SecurePass123" ``` ### Create Database Creator User ```bash ansible-playbook playbooks/create-postgres-user.yml -e "pg_username=dbadmin pg_createdb=true" ``` ### Create Basic User (No Special Privileges) ```bash ansible-playbook playbooks/create-postgres-user.yml -e "pg_username=readonly" ``` ## Playbook Variables | Variable | Required | Default | Description | |----------|----------|---------|-------------| | `pg_username` | Yes | - | PostgreSQL username to create | | `pg_password` | No | Auto-generated | Password (random 32-char base64 if not provided) | | `pg_superuser` | No | `false` | Grant SUPERUSER privilege | | `pg_createdb` | No | `false` | Grant CREATEDB privilege | | `pg_createrole` | No | `false` | Grant CREATEROLE privilege | | `pg_login` | No | `true` | Allow user to login | | `pg_save_credentials` | No | `false` | Save credentials to `/tmp/postgres-user-*.txt` | ## Examples ### Example 1: HiveOps Application User (Superuser) ```bash ansible-playbook playbooks/create-postgres-user.yml \ -e "pg_username=hiveops" \ -e "pg_superuser=true" \ -e "pg_save_credentials=true" ``` **Output:** - Random password generated - Superuser privileges - Credentials saved to `/tmp/postgres-user-hiveops-*.txt` ### Example 2: Application User with Database Creation ```bash ansible-playbook playbooks/create-postgres-user.yml \ -e "pg_username=smartjournal" \ -e "pg_createdb=true" \ -e "pg_password=MySecurePassword123" ``` **Output:** - Specific password used - Can create databases - Cannot create other users ### Example 3: Read-Only Application User ```bash # First create the user ansible-playbook playbooks/create-postgres-user.yml \ -e "pg_username=reporting" # Then grant SELECT permissions manually ansible postgres -m shell \ -a "psql -d mydb -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO reporting;'" \ --become-user=postgres -b ``` ### Example 4: Multiple Users at Once ```bash # Create a variables file cat > /tmp/users.yml <