-
-
Notifications
You must be signed in to change notification settings - Fork 2
shell
GitHub Actions edited this page Feb 13, 2026
·
1 revision
Category: Database Commands
Open an interactive PostgreSQL shell (psql) connected to your nself database.
Provides direct SQL access to your PostgreSQL database for querying, administration, and debugging.
Features:
- ✅ Automatic connection to project database
- ✅ psql with full features (history, tab completion)
- ✅ Hot execution (no restart needed)
- ✅ Environment-aware (connects to correct database per ENV)
- ✅ Run SQL files directly
nself db shell [OPTIONS] [SQL_FILE]| Option | Description |
|---|---|
-c, --command SQL |
Execute single SQL command |
-f, --file FILE |
Execute SQL from file |
--readonly |
Connect as read-only user |
-v, --verbose |
Show detailed output |
| Argument | Description |
|---|---|
SQL_FILE |
Optional: SQL file to execute |
nself db shellOutput:
psql (15.3)
Type "help" for help.
myapp_db=# SELECT * FROM users LIMIT 5;
id | email | created_at
----+--------------------+------------
1 | user1@example.com | 2026-02-01
2 | user2@example.com | 2026-02-02
(2 rows)
myapp_db=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+---------
public | users | table | postgres
public | roles | table | postgres
public | permissions | table | postgres
(3 rows)
myapp_db=# \q
nself db shell -c "SELECT COUNT(*) FROM users;"Output:
count
-------
142
(1 row)
nself db shell -f scripts/analytics.sqlOutput:
→ Executing: scripts/analytics.sql
user_count | order_count | revenue
------------+-------------+---------
142 | 1523 | 45328.50
(1 row)
✓ Script executed successfully
nself db shell --readonlyUse when:
- Querying production data
- Preventing accidental modifications
- Running analytics queries
-- List all tables
\dt
-- Describe table structure
\d users
-- List all schemas
\dn
-- List all databases
\l
-- List all roles
\du
-- Show current connection info
\conninfo
-- List all indexes
\di
-- Execute SQL from file
\i path/to/file.sql
-- Toggle timing
\timing
-- Quit
\q-- Table sizes
SELECT
schemaname,
tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename))
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
-- Active connections
SELECT
pid,
usename,
application_name,
client_addr,
state,
query
FROM pg_stat_activity
WHERE datname = current_database();
-- Database size
SELECT pg_size_pretty(pg_database_size(current_database()));
-- Recent queries
SELECT
query,
calls,
total_time,
mean_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;myapp_db=# SELECT
myapp_db-# users.email,
myapp_db-# COUNT(orders.id) as order_count
myapp_db-# FROM users
myapp_db-# LEFT JOIN orders ON users.id = orders.user_id
myapp_db-# GROUP BY users.email
myapp_db-# HAVING COUNT(orders.id) > 10;BEGIN;
UPDATE users SET status = 'active' WHERE id = 123;
UPDATE user_stats SET last_login = NOW() WHERE user_id = 123;
COMMIT;
-- Or ROLLBACK; to undo-- Export to CSV
\copy (SELECT * FROM users) TO '/tmp/users.csv' CSV HEADER;
-- Import from CSV
\copy users FROM '/tmp/users.csv' CSV HEADER;
-- Export with query
\copy (SELECT * FROM users WHERE created_at > '2026-01-01') TO '/tmp/recent_users.csv' CSV HEADER;nself db shell -c "\d+ users"Shows:
- Column names and types
- Constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE)
- Indexes
- Triggers
- Table size
nself db shell -c "SELECT conname, contype FROM pg_constraint WHERE conrelid = 'users'::regclass;"nself db shell -c "SELECT schemaname, tablename, indexname, idx_scan FROM pg_stat_user_indexes ORDER BY idx_scan DESC;"Error:
psql: error: connection to server failed: Connection refused
Solutions:
# Check if PostgreSQL is running
nself status postgres
# If not running
nself start postgres
# Check port
grep POSTGRES_PORT .envError:
ERROR: permission denied for table users
Solutions:
# Check user permissions
nself db shell -c "\du"
# Grant permissions (as admin)
nself db shell -c "GRANT ALL PRIVILEGES ON TABLE users TO myuser;"Error:
FATAL: database "myapp_db" does not exist
Solutions:
# Check database name in .env
grep POSTGRES_DB .env
# Create database if missing
nself db shell -c "CREATE DATABASE myapp_db;"
# Or reset and reinitialize
nself db reset
nself db migrate upError:
ERROR: canceling statement due to statement timeout
Solutions:
-- Increase timeout for session
SET statement_timeout = '60s';
-- Or globally (requires restart)
ALTER DATABASE myapp_db SET statement_timeout = '60s';nself db shell
# Connects to dev database (from .env.dev or .env.local)nself env switch staging
nself db shell
# Connects to staging databasenself env switch prod
nself db shell --readonly
# Read-only recommended for production# Daily user report
nself db shell -f reports/daily-users.sql > /tmp/daily-report.txt
# Email report
nself db shell -f reports/weekly-stats.sql | mail -s "Weekly Stats" admin@example.com# Check if database is accessible
if nself db shell -c "SELECT 1" > /dev/null 2>&1; then
echo "Database OK"
else
echo "Database ERROR"
exit 1
fi# Verify migrations
nself db shell -c "SELECT version FROM schema_migrations ORDER BY version;"
# Check for data integrity issues
nself db shell -f scripts/integrity-check.sqlBEGIN;
-- Your UPDATE/DELETE statements
-- Verify changes with SELECT
COMMIT; -- Or ROLLBACK if wrong-- Test with small dataset first
SELECT * FROM users WHERE status = 'inactive' LIMIT 5;
-- Then run full query
DELETE FROM users WHERE status = 'inactive';# Backup before major changes
nself db backup before-bulk-update.sql
# Make changes
nself db shell -f bulk-update.sql
# If problems, restore
nself db restore backups/before-bulk-update.sqlEXPLAIN ANALYZE
SELECT * FROM users
WHERE email LIKE '%@example.com'
ORDER BY created_at DESC
LIMIT 100;-
nself db migrate- Apply schema migrations -
nself db seed- Load seed data -
nself db backup- Backup database before queries -
nself db restore- Restore if something goes wrong
ɳSelf CLI v1.0.9. MIT licensed. Docs CC BY 4.0.
GitHub · Issues · Discussions · nself.org · docs.nself.org
Getting Started
Commands
- Commands, Overview
- Lifecycle: cmd-init · cmd-build · cmd-start · cmd-stop · cmd-restart · cmd-dev
- Monitoring: cmd-status · cmd-logs · cmd-health · cmd-urls · cmd-doctor · cmd-monitor · cmd-alerts · cmd-watchdog · cmd-dogfood
- Data: cmd-db · cmd-backup · cmd-dr · cmd-queue · cmd-webhooks
- Config: cmd-config · cmd-service · cmd-env · cmd-promote
- Networking: cmd-ssl · cmd-trust · cmd-dns-setup
- Security: cmd-security · cmd-secrets · cmd-waf
- Tenancy: cmd-tenant · cmd-billing
- Plugins: cmd-plugin · cmd-license
- AI: cmd-ai · cmd-claw
- Utilities: cmd-exec · cmd-clean · cmd-reset · cmd-update · cmd-upgrade · cmd-version · cmd-admin · cmd-migrate · cmd-completion
Features
- Features, Overview
- Feature-Auth
- Feature-Storage
- Feature-Search
- Feature-Functions
- Feature-Email
- Feature-Monitoring
- Feature-Plugins
- Feature-ɳClaw, AI Assistant
- Feature-ɳChat, Messaging
- Feature-ɳTV, Media Player
- Feature-ɳFamily, Family Social
- Feature-ɳCloud, Managed Hosting
- Feature-Memory-Rooms, Knowledge Organization
- Feature-Agent-Dashboard, Agent Metrics
- Feature-Image-Generation, AI Image Generation
Configuration
- Configuration, Overview
- Config-Env-Vars
- Config-Postgres
- Config-Hasura
- Config-Auth
- Config-Nginx
- Config-Optional-Services
- Config-Custom-Services
- Config-System
Plugins (87 + 10 monitoring)
Free (25)
- plugin-backup
- plugin-content-acquisition
- plugin-content-progress
- plugin-cron
- plugin-donorbox
- plugin-feature-flags
- plugin-github
- plugin-github-runner
- plugin-invitations
- plugin-jobs
- plugin-link-preview
- plugin-mdns
- plugin-mlflow
- plugin-monitoring
- plugin-notifications
- plugin-notify
- plugin-paypal
- plugin-search
- plugin-shopify
- plugin-stripe
- plugin-subtitle-manager
- plugin-tokens
- plugin-torrent-manager
- plugin-vpn
- plugin-webhooks
Pro (62)
- plugin-access-controls
- plugin-activity-feed
- plugin-admin-api
- plugin-ai
- plugin-analytics
- plugin-auth
- plugin-backup-pro
- plugin-bots
- plugin-browser
- plugin-calendar
- plugin-cdn
- plugin-chat
- plugin-claw
- plugin-claw-budget
- plugin-claw-news
- plugin-claw-web
- plugin-cloudflare
- plugin-cms
- plugin-compliance
- plugin-cron-pro
- plugin-ddns
- plugin-devices
- plugin-documents
- plugin-donorbox-pro
- plugin-entitlements
- plugin-epg
- plugin-file-processing
- plugin-game-metadata
- plugin-geocoding
- plugin-geolocation
- plugin-google
- plugin-home
- plugin-idme
- plugin-knowledge-base
- plugin-linkedin
- plugin-livekit
- plugin-media-processing
- plugin-meetings
- plugin-moderation
- plugin-mux
- plugin-notify-pro
- plugin-object-storage
- plugin-observability
- plugin-paypal-pro
- plugin-photos
- plugin-podcast
- plugin-post
- plugin-realtime
- plugin-recording
- plugin-retro-gaming
- plugin-rom-discovery
- plugin-shopify-pro
- plugin-social
- plugin-sports
- plugin-stream-gateway
- plugin-streaming
- plugin-stripe-pro
- plugin-support
- plugin-tmdb
- plugin-voice
- plugin-web3
- plugin-workflows
Planned (26)
plugin-auditplugin-blogplugin-checkoutplugin-commerceplugin-drmplugin-exportplugin-flowplugin-importplugin-ldapplugin-mailgunplugin-mediaplugin-oauth-providersplugin-pagesplugin-postmarkplugin-rate-limitplugin-reportsplugin-samlplugin-schedulerplugin-sendgridplugin-ssoplugin-subscriptionplugin-thumbplugin-transcoderplugin-twilioplugin-wafplugin-watermark
Guides
- Guide-Production-Deployment
- Guide-SSL-Setup
- Guide-Multi-Tenancy
- Guide-Security-Hardening
- Guide-Monitoring-Setup
- Guide-Backup-Restore
- Guide-Custom-Services
- Guide-Migration-from-v1
Architecture
Reference
- API-Reference
- reference-error-codes, Error Codes
Licensing
Security
Brand
Operations
- operations/release-cascade, Release Cascade
- operations/self-healing, Self-Healing Schema
- operations/redis-tuning, Redis Pool Tuning
- operations/meilisearch-warmup, MeiliSearch Warm-Up
- operations/jwt-rotation, JWT Key Rotation
- operations/windows-wsl2-setup, Windows / WSL2 Setup
- operations/gemini-oauth-reauth, Gemini OAuth Reauth
Contributing