How to silence PostgreSQL in Rails

Using PostgreSQL for your Rails applications is great, but it tends to be terribly chatty, spewing notices and internal queries all over your terminal and log files.

Luckily, there are a few simple steps to make PostgreSQL a bit more bearable to work with.

Notices when running tests

The most obvious noise from PostgreSQL is when running your tests. For a large’ish application a simple rake will cause pages of

NOTICE:  CREATE TABLE will create implicit sequence "inboxes_id_seq" for serial column ""
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "inboxes_pkey" for table "inboxes"

to be output.

To get rid of those, simply add

min_messages: warning

to your database.yml test environment (ActiveRecord documentation, PostgreSQL documentation).

ActiveRecord attribute queries

The first time Rails (well, ActiveRecord) loads a model it fetches the columns/attributes from the table. This is fine, but it can leave a lot of pointless and quite verbose output in your log files:

Inbox Load (3.7ms)  SELECT "inboxes".* FROM "inboxes" ORDER BY created_at LIMIT 1
  SQL (0.9ms)   SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
 FROM pg_attribute a LEFT JOIN pg_attrdef d
 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attrelid = '"inboxes"'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
 ORDER BY a.attnum

Getting rid of those is as simple as installing the silent-postgres gem, which

“Silences internal diagnostic messages from postgresql connection adapter.”

Ah, silence is golden…