如何使用已安装的hstore扩展名创建新数据库? [英] How to create a new database with the hstore extension already installed?
问题描述
最近我遇到了麻烦,试图在Django中使用hstore。我以这种方式安装了hstore:
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)
天真地以为我的新数据库会包括hstore。情况并非如此:
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)
是否有一种自动拥有hstore的方法
Is there a way to automatically have hstore in a newly created database ?
推荐答案
长话短说:
在template1数据库中安装hstore:
Install hstore in the template1 database:
psql -d template1 -c 'create extension hstore;'
分步说明:
如 PostgreSQL文档:
创建扩展会加载新的扩展名添加到当前数据库中。
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)
如果您有一个以用户名命名的数据库。现在使用 dbtest
:
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)
好,知道了。现在,要创建安装了hstore的新数据库,您必须将其安装在 template1
数据库中。根据文档:
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实际上是通过复制现有数据库来工作的。默认情况下,它会复制名为template1的标准系统数据库。
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1.
让我们这样做:
$ psql -d template1 -c 'create extension hstore;'
并检查其是否有效:
$ 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)
完成!
这篇关于如何使用已安装的hstore扩展名创建新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!