Feature-rich command-line interface tool for managing PostgreSQL databases. Built with asyncpg and featuring beautiful output formatting with Rich, it provides intelligent auto-detection of database configurations from Django settings, environment files, and various configuration formats.
- π Async Operations: Built on asyncpg for high-performance database operations
- π― Intuitive Sub-commands - Natural command structure (e.g.,
show dbs,show tables) - π Auto-detection - Automatically finds and parses Django
settings.pyfiles - π¨ Beautiful Output - Rich formatted tables with color-coded messages
- π Security First - Confirmation prompts for destructive operations, read-only mode for queries
- π Comprehensive Info - Database sizes, table structures, indexes, connections, and more
- π‘οΈ Production Ready - Robust error handling, connection timeouts, transaction management, Error handling, logging, and safety features
pip install psqlc
# or
pip install git+https://github.com/cumulus13/psqlc# Show version
psqlc --version
# List all databases
psqlc show dbs
# List tables in current project's database (auto-detected)
psqlc show tables
# Show database users
psqlc show users
# Show all databases
psqlc show dbs -U postgres -P password
# Show tables in a database
psqlc show tables -d mydb -U postgres
# Create user and database
psqlc create newuser newpass newdb -U postgres
# Execute a query
psqlc query -d mydb -q "SELECT * FROM users LIMIT 10" -U postgresIf you have a Django settings.py file in your project to extract PostgreSQL configuration. It searches:
- Current working directory
- Recursively down to 5 levels deep
- Explicit paths provided as arguments
the tool will automatically detect and use the database configurations from:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'myuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}# No need to specify database credentials!
psqlc show tables
psqlc show dbs
psqlc describe -t users# .env
POSTGRESQL_USERNAME=myuser
POSTGRESQL_PASSWORD=mypassword
POSTGRESQL_DB_NAME=mydatabase
POSTGRESQL_HOST=localhost
POSTGRESQL_PORT=5432{
"engine": "postgresql",
"user": "myuser",
"password": "mypassword",
"database": "mydatabase",
"host": "localhost",
"port": 5432
}The tool searches recursively (up to 5 levels deep) for settings.py|.env|.json|.yaml|.ini files and extracts PostgreSQL configuration automatically.
- Command-line arguments (highest priority)
- Django settings.py -> .env -> .json -> .yaml -> .ini (if no CLI args provided)
- Interactive prompt (for passwords only)
# Specify settings.py file
psqlc create /path/to/settings.py
psqlc create /path/to/*.env
psqlc create /path/to/anyfile.env
psqlc create /path/to/*.json
psqlc create /path/to/anyfile.json
psqlc create /path/to/*.yaml
psqlc create /path/to/anyfile.yaml
psqlc create /path/to/*.ini
psqlc create /path/to/anyfile.ini
# Specify directory containing settings.py
psqlc create /path/to/project/These options can be used with any command:
| Option | Description | Default |
|---|---|---|
-H, --hostname |
PostgreSQL server address | 222.222.222.5 |
-U, --user |
PostgreSQL superuser | postgres |
-P, --passwd |
PostgreSQL superuser password | Auto-detect or prompt |
--port |
PostgreSQL server port | 5432 |
--debug |
Enable debug mode | False |
Display various database information with beautiful formatted tables.
psqlc show dbsShows all databases with sizes, encoding, and collation.
Example Output:
βββββββββββββββββββββ¬ββββββββββ¬ββββββββββββ¬βββββββββββββ
β Database β Size β Encoding β Collation β
βββββββββββββββββββββΌββββββββββΌββββββββββββΌβββββββββββββ€
β myapp_production β 15 MB β UTF8 β en_US.utf8 β
β myapp_development β 8 MB β UTF8 β en_US.utf8 β
β postgres β 8 MB β UTF8 β en_US.utf8 β
βββββββββββββββββββββ΄ββββββββββ΄ββββββββββββ΄βββββββββββββ
List all tables in a database with size and column count.
psqlc show tables -d mydatabase
# Or auto-detect database from settings
psqlc show tablesExample Output:
ββββββββββ¬βββββββββββββββββββ¬βββββββββ¬ββββββββββ
β Schema β Table β Size β Columns β
ββββββββββΌβββββββββββββββββββΌβββββββββΌββββββββββ€
β public β auth_user β 256 KB β 11 β
β public β django_session β 128 KB β 4 β
β public β products_product β 512 KB β 8 β
ββββββββββ΄βββββββββββββββββββ΄βββββββββ΄ββββββββββ
Options:
-d, --database- Database name (auto-detects from settings.py if not provided)
List all PostgreSQL users/roles with their permissions.
psqlc show usersOutput:
ββββββββββββββββ¬ββββββββββββ¬ββββββββββββ¬ββββββββββββββ¬ββββββββββββ¬βββββββββββββββ
β Username β Superuser β Create DB β Create Role β Can Login β Replication β
ββββββββββββββββΌββββββββββββΌββββββββββββΌββββββββββββββΌββββββββββββΌβββββββββββββββ€
β postgres β True β True β True β True β True β
β myapp_user β False β True β False β True β True β
ββββββββββββββββ΄ββββββββββββ΄ββββββββββββ΄ββββββββββββββ΄ββββββββββββ΄βββββββββββββββ
Display active database connections with client information.
psqlc show connectionsDisplay indexes in database or specific table.
# Show all indexes in database
psqlc show indexes
# Show indexes for specific table
psqlc show indexes -d mydb -t usersOptions:
-d, --database- Database name (auto-detect if not provided)-t, --table- Table name (optional, shows all if not provided)
Display size information for databases or tables.
# Show all database sizes
psqlc show size
# Show table sizes in a database
psqlc show size -d mydb
# Show size of specific table
psqlc show size -d mydb -t usersOptions:
-d, --database- Database name (auto-detect if not provided)-t, --table- Table name (optional, shows all tables if not provided)
Example Output:
π Size of table 'users':
Total Size: 15 MB
Table Size: 12 MB
Indexes Size: 3 MB
Create a new PostgreSQL user and database with proper privileges.
# Method 1: Direct arguments
psqlc create username password dbname -U postgres
# Method 2: Named arguments
psqlc create -u username -p password -d dbname -U postgres
# Method 3: Auto-detect from settings.py
psqlc create -U postgres
# Method 4: From Django settings file
psqlc create /path/to/settings.py -U postgresOptions:
CONFIG- Positional arguments:NEW_USERNAME NEW_PASSWORD NEW_DB-u, --username- New PostgreSQL username-p, --password- Password for new user-d, --database- Database name to create
Features:
- Automatically creates user with LOGIN, CREATEDB, REPLICATION, BYPASSRLS privileges
- Checks for existing database and prompts for confirmation before dropping
- Supports Django settings.py auto-detection
Show detailed table structure including columns, data types, and constraints.
# Auto-detect database
psqlc describe -t users
# Specify database
psqlc describe -d mydb -t usersOptions:
-d, --database- Database name (auto-detect if not provided)-t, --table- Table name (required)
Output:
ββββββββββββββββ¬βββββββββββββββ¬βββββββββββββ¬βββββββββββ¬ββββββββββββββ
β Column β Type β Max Length β Nullable β Default β
ββββββββββββββββΌβββββββββββββββΌβββββββββββββΌβββββββββββΌββββββββββββββ€
β id β integer β - β NO β nextval() β
β username β varchar β 150 β NO β - β
β email β varchar β 255 β YES β - β
β created_at β timestamp β - β NO β now() β
ββββββββββββββββ΄βββββββββββββββ΄βββββββββββββ΄βββββββββββ΄ββββββββββββββ
Execute custom SQL queries with safety features.
# Basic query
psqlc query -d mydb -q "SELECT * FROM users LIMIT 10"
# Read-only mode (prevents destructive operations)
psqlc query -d mydb -q "SELECT * FROM users" --readonly
# Limit rows displayed
psqlc query -d mydb -q "SELECT * FROM logs" --limit 50Options:
-d, --database- Database name (auto-detect if not provided)-q, --query- SQL query to execute (required)--readonly- Prevent destructive operations (blocks DROP, DELETE, TRUNCATE, ALTER, CREATE, INSERT, UPDATE)--limit- Limit number of rows displayed (default: 100)
Safety Features:
- Read-only mode blocks all destructive SQL commands
- Automatic transaction rollback on errors
- Row limit prevents memory issues with large result sets
Generate backup command for a database using pg_dump.
# Auto-detect database
psqlc backup
# Specify database
psqlc backup -d mydbpsqlc backup -d mydatabaseOutput:
ποΈ Creating backup of 'mydatabase'...
π‘ Run this command manually:
pg_dump -h 127.0.0.1 -p 5432 -U postgres -d mydatabase -F p -f mydatabase_backup_20231014_143022.sql
Options:
-d, --database- Database name to backup (auto-detect if not provided)
Note: This command generates the pg_dump command for you to run manually. Direct backup execution requires pg_dump to be installed and accessible in your PATH.
Safely drop databases or users with confirmation prompts.
Drop a database with safety confirmation.
# Auto-detect database
psqlc drop database
# Specify database
psqlc drop database -d mydbOptions:
-d, --database- Database name to drop (auto-detect if not provided)
Safety Features:
- Requires typing the exact database name for confirmation
- Automatically terminates all active connections before dropping
- Cannot be undone - use with caution!
Drop a PostgreSQL user/role with safety confirmation.
psqlc drop user -u usernameOptions:
-u, --username- Username to drop (required)
Safety Features:
- Requires typing the exact username for confirmation
- Cannot be undone - use with caution!
# Use specific settings file
psqlc --debug show tables
# Custom host and port
psqlc -H db.example.com --port 5433 show dbs
# Different superuser
psqlc -U admin show usersEnable debug mode for detailed logging:
psqlc --debug show dbsOr set environment variable:
export DEBUG=1
psqlc show tablesParse Django settings.py or config files for database configuration.
Parameters:
settings_path: Optional path to settings file
Returns: Database configuration dictionary or None
get_connection(host: str, port: int, user: str, password: str, database: str = "postgres", auto_settings: bool = True, settings_path = None)
Create async database connection.
List all databases with detailed information.
Create PostgreSQL user and database with proper privileges.
psqlc includes comprehensive error handling:
- Connection failures are clearly reported
- Invalid queries show descriptive errors
- Destructive operations require confirmation
- Debug mode provides detailed error information
# Create user and database from Django settings
psqlc create -U postgres
# Verify creation
psqlc show dbs
psqlc show users
# Check table structure
psqlc show tables
psqlc describe -t auth_user# Check database sizes
psqlc show size
# Check specific database details
psqlc show size -d mydb
psqlc show tables -d mydb
psqlc show indexes -d mydb
# Monitor connections
psqlc show connections# Read-only query (safe)
psqlc query -q "SELECT COUNT(*) FROM users" --readonly
# Regular query with limit
psqlc query -q "SELECT * FROM logs ORDER BY created_at DESC" --limit 20
# Complex query
psqlc query -q "
SELECT u.username, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.username
ORDER BY order_count DESC
" --readonly# Backup database
psqlc backup -d production_db
# Check sizes before cleanup
psqlc show size -d production_db
# Drop old test database
psqlc drop database -d old_test_db
# Drop old user
psqlc drop user -u old_test_user# 1. Check existing databases
psqlc show dbs
# 2. Create new user and database
psqlc create -u myapp -p mypass123 -d myapp_db
# 3. List tables in new database
psqlc show tables -d myapp_db
# 4. Execute setup queries
psqlc query -d myapp_db -q "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100))"
# 5. Check table structure
psqlc describe -d myapp_db -t users
# 6. Monitor database size
psqlc show size -d myapp_db
# 7. Generate backup command
psqlc backup -d myapp_dbWhen working in a Django project directory, psqlc automatically detects your settings.py:
cd /path/to/your/django/project
psqlc show tables # Auto-detects database from settings.py
psqlc describe -t auth_user # No need to specify database# Monitor active connections
psqlc show connections
# Check database sizes regularly
psqlc show size
# Monitor user activity
psqlc show users- Never hardcode passwords - Use environment variables or settings files
- Use read-only mode for SELECT queries in production
- Always confirm before dropping databases or users
- Limit query results to prevent memory issues
- Use connection timeouts (built-in: 10 seconds)
- Regular backups before major operations
psqlc --debug show dbsexport TRACEBACK=1
psqlc show dbsDebug mode shows:
- Settings.py detection attempts
- Connection details
- Detailed error messages
- full traceback
# Check if PostgreSQL is running
psqlc -H localhost --port 5432 show dbs- Check hostname, port, username, and password
- Verify PostgreSQL server is running
- Check firewall settings
# Provide password explicitly
psqlc -U postgres -P yourpassword show dbs- Ensure settings.py exists in current directory or subdirectories
- Try specifying the path explicitly
- Use
--debugto see search paths
# Specify settings file directly
psqlc --debug show tables- Verify user has sufficient privileges
- Some operations require superuser access
- Check PostgreSQL user permissions
| Variable | Description | Default |
|---|---|---|
DEBUG |
Enable debug output | 0 |
TRACEBACK |
Show full tracebacks on errors | 0 |
HOST |
Database host | empty/None |
PORT |
Database port | 5432 |
USER |
Database user | postgres |
PASSWORD |
Database password | empty/None |
DATABASE/DB_NAME/DB |
Database name | empty/None |
Set environment variables:
# Linux/Mac
export DEBUG=1
psqlc show dbs
# Windows
set DEBUG=1
psqlc show dbsContributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with psycopg2 for PostgreSQL connectivity
- Styled with Rich for beautiful terminal output
- Argument parsing with licface for custom help formatting
For issues, questions, or contributions, please visit the project repository or contact the author.
Note: This tool is designed for PostgreSQL database management. Always test commands in a development environment before using in production.