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
/usr/local/pgsql/share/contrib/pgcrypto.sql
并告诉我们。