docs/README_READ_ONLY_DATABASE.md
This application now supports using a separate read-only database for executing user queries. This provides better security, performance, and scalability for user query operations.
The read-only database feature allows you to:
Set the following environment variable to enable read-only database support:
READ_ONLY_DATABASE_URL=postgres://username:password@host:port/database_name
READ_ONLY_DATABASE_POOL_SIZE - Connection pool size (default: 5)READ_ONLY_STATEMENT_TIMEOUT - Query timeout in milliseconds (default: 30000)# Production example
READ_ONLY_DATABASE_URL=postgres://readonly_user:[email protected]:5432/forem_readonly
READ_ONLY_DATABASE_POOL_SIZE=10
READ_ONLY_STATEMENT_TIMEOUT=60000
-- Create a read-only user
CREATE USER readonly_user WITH PASSWORD 'secure_password';
-- Grant connect permission
GRANT CONNECT ON DATABASE your_database TO readonly_user;
-- Grant usage on schema
GRANT USAGE ON SCHEMA public TO readonly_user;
-- Grant SELECT on all tables
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;
-- Grant SELECT on future tables
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_user;
For production environments, set up a read replica:
# Using PostgreSQL streaming replication
# This is database-specific and depends on your setup
If the READ_ONLY_DATABASE_URL environment variable is not set, the system automatically falls back to using the main database for user queries.
Access the read-only database status at /admin/read_only_database to:
# Check read-only database health
bundle exec rake read_only_database:health_check
# Test with a simple query
bundle exec rake read_only_database:test_query
# Reset connection pool
bundle exec rake read_only_database:reset_pool
The system provides logging for read-only database usage:
# Debug logs show which database is being used
"Using read-only database for user query execution"
"Read-only database not configured, using main database for user query execution"
READ_ONLY_DATABASE_URL formatREAD_ONLY_DATABASE_POOL_SIZE if neededUse the admin interface or rake tasks to monitor:
If you're currently using the main database for user queries, the migration is seamless:
For issues or questions about the read-only database feature:
/admin/read_only_database