# Replace YOUR_APP_NAME with your Heroku app name
heroku pg:info -a YOUR_APP_NAME
# Check database connections
heroku pg:connections -a YOUR_APP_NAME# On Heroku
heroku run rails runner "puts ActiveRecord::Base.connection.execute('SELECT 1').first"
# Test connection speed
heroku run rails runner "start=Time.now; ActiveRecord::Base.connection.execute('SELECT 1'); puts 'Connection time: #{(Time.now - start) * 1000}ms'"# On Heroku
heroku run rake db:diagnose# Watch logs in real-time
heroku logs --tail -a YOUR_APP_NAME
# Look for database-related errors
heroku logs --tail -a YOUR_APP_NAME | grep -i "database\|connection\|timeout\|pg"# See what queries are running (if pg_stat_statements is enabled)
heroku pg:psql -a YOUR_APP_NAME -c "SELECT pid, state, query FROM pg_stat_activity WHERE state != 'idle';"The timeout occurs when logged_in? is called. With Sorcery's remember_me submodule enabled, logged_in? does:
- Checks session for user (fast, no DB query)
- If no session, checks
remember_mecookie - If cookie exists, queries
remember_tokenstable to validate the token - Queries
userstable to load the user
The problem: If there's a stale/expired remember_me cookie, the query to remember_tokens might be hanging or timing out.
- Check if all connections are checked out
- Run:
heroku run rake db:diagnoseand look at "Connection Pool Status"
- Check query execution time in diagnostics
- Look for queries taking > 1000ms
- Check if there are many expired/invalid tokens
- The query might be scanning a large table
- Verify
DATABASE_URLis correctly set - Check connection parameters
- Verify the database is not in maintenance mode
- Check for connection limits reached
- If connection pool is exhausted: Reduce pool size or check for connection leaks
- If queries are slow: Add indexes, check for table locks
- If remember_tokens table is the issue: Clean up expired tokens or optimize the query
- If database URL is wrong: Fix
DATABASE_URLenvironment variable