我正在使用Ubuntu Server 10.10 apt-get install postgresql. 。我想使用内置 sha1() 功能,但似乎我必须安装 pgcrypto 第一的。但是我不知道如何安装它。

没有 pgcrypto 如果我尝试使用 apt-get install pgcrypto 而且我找不到任何文件 pgcrypto 在我的系统中(我尝试了 find / -name "pgcrypto*").

我如何安装pgcrypto,以便我可以使用 digest('word-to-hash','sha1') 我的数据库查询中的功能?


更新: 我正在努力在另一台Ubuntu机器上安装PGCRYPTO。在安装软件包后使用 sudo apt-get install postgresql-contrib-8.4 如何将其安装到当前的PostgreSQL数据库中?

有帮助吗?

解决方案

对于较新版本的PG,请查看下面的答案Dustin Kirkland

这是Postgres的外部模块。您应该安装 postgresql-contrib-8.4 (或您的PG版本)通过APT:

apt-get install postgresql-contrib-8.4

然后,您在某处找到SQL安装文件 /usr/share/postgresql 文件夹,您需要运行 pgcryto.sql 在数据库上。

psql -d <database> -f /usr/share/postgresql/8.4/contrib/pgcrypto.sql

或者,

$ cd /usr/share/postgresql/8.4/contrib
$ psql -d <database>
    psql (8.4.8)
    Type "help" for help.

    database=# \i pgcrypto.sql

其他提示

Postgresql 9.1+

请注意,我正在使用使用PostgreSQL 9.1的Ubuntu 12.04。

在那里,我需要:

sudo apt-get install postgresql-contrib

然后在我的数据库中:

postgres@ztrustee:~$ psql test
psql (9.1.3)
Type "help" for help.
test=# CREATE EXTENSION pgcrypto;
CREATE EXTENSION

现在,我可以使用pgcrypto功能,gen_random_bytes():

test=# create table test ( 
  id 
    text 
    not null 
    default encode( gen_random_bytes( 32 ), 'hex' ) 
    primary key, 
  value 
    text 
); 
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
test=# \d test
                            Table "public.test"
 Column | Type |                         Modifiers                          
--------+------+------------------------------------------------------------
 id     | text | not null default encode(gen_random_bytes(32), 'hex'::text)
 value  | text | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id)

test=# insert into test (value) VALUES ('scoobydoo');
INSERT 0 1
test=# select * from test;
                                id                                |   value   
------------------------------------------------------------------+-----------
 76dd5bd0120d3df797f932fbcb4f8aa5088e215ee2b920dddbff59c8595fbac7 | scoobydoo

对于最新版本,pgcrypto.sql没有文件路径结束。

在所需的用户下创建扩展名PGCRYPTO。

$ psql -U <username> -d mydb

psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))
Type "help" for help.

mydb=> CREATE EXTENSION pgcrypto;

CREATE EXTENSION
mydb=> 

如果用户无权创建扩展名,请通过登录为postgres(默认)用户允许超级用户,然后重试。

$ psql --u postgres

psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# ALTER USER <username> WITH SUPERUSER;

ALTER ROLE
许可以下: CC-BY-SA归因
不隶属于 dba.stackexchange
scroll top