我使用的是UbuntuServer10.10,我已经使用apt-get install postgresql安装了apt-get install postgresql 8.4。我想使用内置的sha1()函数,但似乎我必须先安装pgcrypto。但我不知道怎么安装。
如果我尝试使用pgcrypto安装它,并且在我的系统中找不到以pgcrypto开头的任何文件(我尝试了find / -name "pgcrypto*"),就不会有任何文件。
如何安装pgcrypto以便在数据库查询中使用digest('word-to-hash','sha1')函数?
更新:我很难在另一台Ubuntu机器上安装pgcrypto。使用sudo apt-get install postgresql-contrib-8.4安装软件包后,如何将其安装到当前的PostgreSQL数据库中?
发布于 2011-03-24 14:35:53
关于PG的更新版本,请看下面由Dustin Kirkland提供的答案。
它是Postgres的外部模块。您应该通过apt安装postgresql-contrib-8.4 (或pg版本)包:
apt-get install postgresql-contrib-8.4然后在/usr/share/postgresql文件夹中的某个位置找到sql文件,并且需要在数据库上运行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发布于 2012-03-30 17:51:49
注意,我正在开发Ubuntu12.04,它使用PostgreSQL9.1。
在那里,我需要:
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发布于 2019-02-13 09:46:21
对于最新版本,没有以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 ROLEhttps://dba.stackexchange.com/questions/1883
复制相似问题