如何使用已安装的hstore扩展名创建新数据库?

编程入门 行业动态 更新时间:2024-10-21 10:18:40
本文介绍了如何使用已安装的hstore扩展名创建新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

最近我遇到了麻烦,试图在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扩展名创建新数据库?

本文发布于:2023-10-13 10:04:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1487644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:扩展名   如何使用   数据库   hstore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!