I just do this in a migration
class AddCryptoToDb < ActiveRecord::Migration
def up
execute <<-SQL
CREATE extension IF NOT EXISTS pgcrypto;
SQL
end
end
You can execute any sql want there. I also do it for functions
class BuildFnSetWebUsersUid < ActiveRecord::Migration
def up
say "building fn_set_web_users_uid function"
execute <<-SQL
CREATE OR REPLACE FUNCTION fn_set_web_users_uid()
RETURNS trigger AS
$BODY$
BEGIN
IF NEW.uid IS NULL THEN
SELECT UID into NEW.uid
FROM generate_series(10000, (SELECT last_value FROM web_users_uid_seq)) AS s(uid)
EXCEPT
SELECT uid FROM web_users
ORDER BY uid;
IF NEW.uid is NULL THEN
SELECT nextval('web_users_uid_seq') INTO NEW.uid;
END IF;
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql';
SQL
end
def down
execute "DROP FUNCTION IF EXISTS fn_set_web_users_uid;"
end
end
Also it doesn't need to be postgres user, depending upon the extension it needs to be superuser or database owner. So on my dev boxes I give the user Super User Rights for ease of use.