Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new database with the hstore extension already installed?

Recently I went into trouble trying to use hstore with Django. I installed hstore this way:

$ sudo -u postgres psql postgres=# CREATE EXTENSION hstore; WARNING:  => is deprecated as an operator name DETAIL:  This name may be disallowed altogether in future versions of PostgreSQL. CREATE EXTENSION postgres=# \dx                            List of installed extensions   Name   | Version |   Schema   |                   Description                     ---------+---------+------------+--------------------------------------------------  hstore  | 1.0     | public     | data type for storing sets of (key, value) pairs  plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language (2 rows) 

And naively thought that my new databases would include hstore. That ain't the case:

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

Is there a way to automatically have hstore in a newly created database ?

like image 268
Maxime R. Avatar asked Jul 20 '12 18:07

Maxime R.


People also ask

How do I use Hstore in PostgreSQL?

The hstore module is used to implement the hstore data type in the form of key-value pairs for a single value within PostgreSQL. The hstore data type is remarkably effective in many cases, such as, multiple rows with multiple attributes which are rarely queried for or semi-structured data.

What is Hstore extension?

hstore is a PostgreSQL extension that implements the hstore data type. It's a key-value data type for PostgreSQL that's been around since before JSON and JSONB data types were added.

What is create extension in PostgreSQL?

Description. CREATE EXTENSION loads a new extension into the current database. There must not be an extension of the same name already loaded. Loading an extension essentially amounts to running the extension's script file.


1 Answers

Long story short:

Install hstore in the template1 database:

psql -d template1 -c 'create extension hstore;' 

Step-by-step explanation:

As stated by the PostgreSQL documentation:

CREATE EXTENSION loads a new extension into the current database.

Installing an extension is database-specific. The following returns you the current database name:

$ psql -c 'select current_database()'  current_database  ------------------  username (1 row) 

In case you have a database named after your username. Now with dbtest:

$ psql -d dbtest -c 'select current_database()'  current_database  ------------------  dbtest (1 row) 

Ok, you got it. Now, to create new databases with hstore installed, you'll have to install it in the template1 database. According to the doc:

CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1.

Let's do this:

$ psql -d template1 -c 'create extension hstore;' 

And check that it works :

$ createdb dbtest $ psql -d dbtest -c '\dx'                  List of installed extensions   Name   | Version |   Schema   |                   Description                     ---------+---------+------------+--------------------------------------------------  hstore  | 1.0     | public     | data type for storing sets of (key, value) pairs  plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language (2 rows) 

Done!

like image 57
Maxime R. Avatar answered Oct 14 '22 22:10

Maxime R.