os: centos 7.4
postgresql: 9.16
postgresql 自带的 extension 叫做 uuid-ossp,是用来产生通用唯一标识符。
安装uuid依赖包
# yum install uuid uuid-devel
configure 时添加 –with-uuid=ossp
# cd /tmp
# wget https://ftp.postgresql.org/pub/source/v9.6.9/postgresql-9.6.9.tar.gz
# tar -zxvf ./postgresql-9.6.9.tar.gz
# cd postgresql-9.6.9
# ./configure --prefix=/usr/pgsql-9.6 --with-perl --with-tcl --with-python --with-openssl --with-pam --without-ldap --with-libxml --with-libxslt --with-uuid=ossp
# make world
# make install-world
如果没有安装os的 uuid、uuid-devel,就会报如下错误。
checking for uuid_export in -lossp-uuid... no
checking for uuid_export in -luuid... no
configure: error: library 'ossp-uuid' or 'uuid' is required for OSSP UUID
查看 uuid-ossp
# cd /usr/pgsql-9.6/lib/
# ls -l |grep -i uuid
-rwxr-xr-x 1 root root 14384 Jun 21 18:25 uuid-ossp.so
# cd /usr/pgsql-9.6/share/extension/
# ls -l |grep -i uuid
-rw-r--r-- 1 root root 688 Jun 21 18:25 uuid-ossp--1.0--1.1.sql
-rw-r--r-- 1 root root 1516 Jun 21 18:25 uuid-ossp--1.1.sql
-rw-r--r-- 1 root root 163 Jun 21 18:25 uuid-ossp.control
-rw-r--r-- 1 root root 853 Jun 21 18:25 uuid-ossp--unpackaged--1.0.sql
创建 uuid-ossp extension
初始化数据库集群略。
postgres=# create database peiybdb;
CREATE DATABASE
postgres=# c peiybdb
You are now connected to database "peiybdb" as user "postgres".
peiybdb=# select * from pg_available_extensions where name like '%uuid%';
name | default_version | installed_version | comment
-----------+-----------------+-------------------+-------------------------------------------------
uuid-ossp | 1.1 | | generate universally unique identifiers (UUIDs)
(1 row)
peiybdb=# create extension "uuid-ossp";
CREATE EXTENSION
peiybdb=# df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+--------------------+------------------+---------------------------+--------
public | uuid_generate_v1 | uuid | | normal
public | uuid_generate_v1mc | uuid | | normal
public | uuid_generate_v3 | uuid | namespace uuid, name text | normal
public | uuid_generate_v4 | uuid | | normal
public | uuid_generate_v5 | uuid | namespace uuid, name text | normal
public | uuid_nil | uuid | | normal
public | uuid_ns_dns | uuid | | normal
public | uuid_ns_oid | uuid | | normal
public | uuid_ns_url | uuid | | normal
public | uuid_ns_x500 | uuid | | normal
(10 rows)
这些函数的使用可以参考官网的uuid-ossp描述。
参考:
http://postgres.cn/docs/9.6/contrib.html
http://postgres.cn/docs/9.6/uuid-ossp.html