Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

sql - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...