http://chenxiaoyu.org/blog/archives/209
測試環境:PostgreSQL 8.4
默認PostgreSQL是木有UUID函數可使用, 而不像MySQL提供uuid()函數,不過在contrib裏有,只需要導入一下uuid-ossp.sql即可。(PS:注意權限問題,要Pg可讀改 文件。)
導入很簡單,下面是win下面測試,其他平臺類似該操 作:
D:/>psql -U postgres -h localhost -f D:/PostgreSQL/8.4/share/contrib/uuid-ossp.sql
Password for user postgres:
SET
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
CREATE FUNCTION
進入psql,執行:
postgres=# select uuid_generate_v1();
uuid_generate_v1
--------------------------------------
86811bd4-22a5-11df-b00e-ebd863f5f8a7
(1 row)
postgres=# select uuid_generate_v4();
uuid_generate_v4
--------------------------------------
5edbfcbb-1df8-48fa-853f-7917e4e346db
(1 row)
主要就是uuid_generate_v1和 uuid_generate_v4,當然還有uuid_generate_v3和uuid_generate_v5。其他使用可以參見 PostgreSQL官方文檔 uuid-ossp 。