Search code examples
ruby-on-railspostgresqlruby-on-rails-4pghstore

How to make Postgresql Hstore work with rails 4 on ubuntu


So I read a few articles that I found in google how to make hstore make with Rails 4, and as it happens, it wasnt that easily as it was told so I decided to describe the steps that I had to take to make it work on UBUNTU machine...


Solution

  • The steps to activate and use Hstore:

    1) You need to create a migration with something like this:

    def change
      enable_extension "hstore"
    end
    

    2) As it was for me, I had to install the thing called

    postgresql-contrib

    on my ubuntu machine, which I did with apt-get command. Perhaps you may need to add a version of your postgresql to the end of the packet name, like this:

    postgresql-contrib-9.3

    3) Now you can add migrations that add "hstore" fields to the tables, notice that these migrations should be following after the one that activates Hstore

    4) Before u run the migrations, you might have to change to the root user of your DB and give the user that u use to access the db (most likely used in the database.yml file) an allowance to add this kind of columns, I did it with the following commands:

    sudo -u postgres psql

    (where postgres is the root user of the db )

    and in the command line of the database write:

    alter user [rails account name] with superuser;

    5) Run the migrations

    6) Change the user allowance back to the way it was with the following command:

    alter user [rails account name] with nosuperuser;

    That's it, now u should be able to use the hstore...

    PS I am a bit of a newbie so perhaps I made some mistakes or something, but I hope its all fine :)