postgres hstore exists and doesn't exist at same time

Tomoko Yamaguchi picture Tomoko Yamaguchi · Oct 19, 2013 · Viewed 11.1k times · Source

I set up a Rails app on a remote server and created an hstore extension

 sudo -u postgres psql
 CREATE EXTENSION hstore;

I then deployed an iteration of the app that uses hstore in one of the postgres tables, but when it ran the migrations it gave an error message

PG::UndefinedObject: ERROR:  type "hstore" does not exist

I then tried to do this again

 sudo -u postgres psql
 CREATE EXTENSION hstore;

but it told me hstore already exists

ERROR:  extension "hstore" already exists

and this circle continued on.

Any idea what might be causing this problem? I'm using postgres 9.1 on an Ubuntu 12.04 server

Update Note, wondering if this issue was related to permissions, I tried to check my permissions like this but got the following error

sudo -u postgres psql -U username
psql: FATAL:  Peer authentication failed for user "username"

Update Although hstore is installed, it's not an extension for the database I'm using. How to install it in a specific database?

psql -d db_production -c '\dx'
                 List of installed extensions
  Name   | Version |   Schema   |         Description          
---------+---------+------------+------------------------------
 plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

Answer

kik picture kik · Oct 19, 2013

To create extension in your database, you have to explicitly connect to that database. So, if your database is my_app_development, you have to do :

sudo -u postgres psql my_app_development
create extension hstore;

Also, you do not tell which rails version you're on. If you're not on rails-4, you will have to use the postgres hstore gem.