PostgreSQL + rake db:migrate

ActiveRecord::NoDatabaseError: FATAL: role “dev” does not exist

If you receive the error above when trying to run your Rails migrations against a newly installed instance of PostgreSQL on an Ubuntu development box, all you need to do is a new role to Postgres with the name ‘dev’. Once the role has been added and given the proper permissions, your migrations should run without issue.

 

Rails 4: gem install pg (when using the Postgresql App on OS X)

If you are getting a build error from Gem when trying to install the PostgreSQL gem called ‘pg’ and you are using the PostgreSQL App instead of installing PostgreSQL via Brew or some other method, it is because Gem cannot find the PostgreSQL config in the standard locations. To correct this you need to provide Gem with the path to the config file stored within the PostgreSQL App’s internal folder structure.

This can be done as follows:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config

Another way to accomplish this is:

export CONFIGURE_ARGS=”with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include/”
gem install pg

Note: the latest version of the PostgreSQL App, at the time of this writing was 9.4, your version may be different. Make sure you change the 9.4 in the config path in the command above to the one you are using or it will not work. You can verify the path by opening Finder, going to the Applications folder, locating Postgres.app, right clicking and selecting ‘show package contents’. This will open up the app’s folder structure in Finder.

ActiveRecord: Multiple Database Configuration

Perhaps you have a web application which not only has a database in which it stores its content but also has one to hold logging data (Exceptions, Log Entries).
This can be a common practice used to keep unrelated data separate and to off load the load created by logging data for purely health monitoring reasons.
If you are using Castle ActiveRecord as your ORM (Object Relational Mapping) layer and are familiar with setting it up for one database you may not be familiar with configuring it to handle more than one database for your objects.
ActiveRecord decides which database to send an object to based on a combination of a Abstract base class which inherits from ActiveRecordBase and a separate configuration section for each of those base classes. Each of those base classes must also be registered with ActiveRecord at application startup just like any Entity you plan on using within the application.
Below are the steps you need to take to properly setup your ASP.net web application to use 2 databases.
1. Create a ActiveRecordBase class for each of the databases you will be using. Each entity which inherits from that base class will be mapped to the corresponding database.

public abstract class ApplicationActiveRecordBase : ActiveRecordBase{}

public abstract class LoggingActiveRecordBase : ActiveRecordBase{}

2. Initialize each of the base classes so that ActiveRecord is aware of the classes

        static ActiveRecordInitializer()
        {
            IConfigurationSource source = ActiveRecordSectionHandler.Instance;
            ActiveRecordStarter.Initialize(
                source,
                typeof(LoggingActiveRecordBase),
                typeof(ApplicationActiveRecordBase));
        }

3. All entities belonging to a specific database should inherit from the base class associated with that database in the configuration.

    [DataContract]
    [ActiveRecord(Table = "exceptionentry")]
    public class ExceptionEntry : LoggingActiveRecordBase<ExceptionEntry>
    {

4. Add configuration sections to the ActiveRecord configuration for each database specifying the ActiveRecordBase class that will be used to identify the database.

  <activerecord isWeb="true" isDebug="false" threadinfotype="Castle.ActiveRecord.Framework.Scopes.HybridWebThreadScopeInfo, Castle.ActiveRecord">
 
    <config type="ActiveRecord.BaseClasses.ApplicationActiveRecordBase`1, Data.Core, Version=1.0.0.0, Culture=neutral">
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
      <add key="hibernate.connection.connection_string" value="Server=localhost; Database=YOURAPPLICATIONDATABASE; User Id=YOURUSER; Password=YOURPASSWORD" />
    </config>
 
    <config type="ActiveRecord.BaseClasses.LoggingActiveRecordBase`1, Data.Core, Version=1.0.0.0, Culture=neutral">
      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="hibernate.dialect" value="NHibernate.Dialect.MySQLDialect" />
      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver" />
      <add key="hibernate.connection.connection_string" value="Server=localhost; Database=YOURLOGGINGDATABASE; User Id=YOURUSER; Password=YOURPASSWORD" />
    </config>
 
  </activerecord