重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 126|回复: 0
打印 上一主题 下一主题

如何在PG14中安装uuid功能

[复制链接]
跳转到指定楼层
楼主
发表于 2024-5-7 21:22:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 郑全 于 2024-5-7 21:23 编辑

环境:
     PG14 +LINUX 8.8

问题:
   开发人员问,PG中,支持 生成UUID吗,具体是什么,答,类似这个函数  uuid_generate_v4()
   看了一下, select uuid_generate_v4();会报错,找不到该函数
   原来postgresql 14 默认是没有该功能,但是可以通过扩展uuid-ossp来实现

解决办法:
   由于PG是通过源码安装的,
   源码中已经有这个源码包了,不需要到处去找
  位置在:
  /postgresql/soft/postgresql-14.8/contrib/uuid-ossp


                               
登录/注册后可看大图




  1. 检查是否已安装扩展uuid-ossp

   \dx

   postgres=# \dx
                 List of installed extensions
  Name   | Version |   Schema   |         Description         
---------+---------+------------+------------------------------
plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
(1 row)

   可以看到postgresql目前没有安装uuid-ossp扩展。

   2.检查是否有可用来安装的扩展uuid-ossp

   --查看当前可用的扩展

  1. postgres=# select * from pg_available_extensions;

  2.         name        | default_version | installed_version |                                comment                                 

  3. --------------------+-----------------+-------------------+------------------------------------------------------------------------

  4. plpgsql            | 1.0             | 1.0               | PL/pgSQL procedural language

  5. plperl             | 1.0             |                   | PL/Perl procedural language

  6. plperlu            | 1.0             |                   | PL/PerlU untrusted procedural language

  7. plpython2u         | 1.0             |                   | PL/Python2U untrusted procedural language

  8. plpythonu          | 1.0             |                   | PL/PythonU untrusted procedural language

  9. adminpack          | 2.1             |                   | administrative functions for PostgreSQL

  10. amcheck            | 1.3             |                   | functions for verifying relation integrity

  11. bloom              | 1.0             |                   | bloom access method - signature file based index

  12. btree_gin          | 1.3             |                   | support for indexing common datatypes in GIN

  13. btree_gist         | 1.6             |                   | support for indexing common datatypes in GiST

  14. citext             | 1.6             |                   | data type for case-insensitive character strings

  15. cube               | 1.5             |                   | data type for multidimensional cubes

  16. dblink             | 1.2             |                   | connect to other PostgreSQL databases from within a database

  17. dict_int           | 1.0             |                   | text search dictionary template for integers

  18. dict_xsyn          | 1.0             |                   | text search dictionary template for extended synonym processing

  19. earthdistance      | 1.1             |                   | calculate great-circle distances on the surface of the Earth

  20. file_fdw           | 1.0             |                   | foreign-data wrapper for flat file access

  21. fuzzystrmatch      | 1.1             |                   | determine similarities and distance between strings

  22. hstore             | 1.8             |                   | data type for storing sets of (key, value) pairs

  23. intagg             | 1.1             |                   | integer aggregator and enumerator (obsolete)

  24. intarray           | 1.5             |                   | functions, operators, and index support for 1-D arrays of integers

  25. isn                | 1.2             |                   | data types for international product numbering standards

  26. lo                 | 1.1             |                   | Large Object maintenance

  27. ltree              | 1.2             |                   | data type for hierarchical tree-like structures

  28. old_snapshot       | 1.0             |                   | utilities in support of old_snapshot_threshold

  29. pageinspect        | 1.9             |                   | inspect the contents of database pages at a low level

  30. pg_buffercache     | 1.3             |                   | examine the shared buffer cache

  31. pg_freespacemap    | 1.2             |                   | examine the free space map (FSM)

  32. seg                | 1.4             |                   | data type for representing line segments or floating-point intervals

  33. pg_prewarm         | 1.2             |                   | prewarm relation data

  34. pg_stat_statements | 1.9             |                   | track planning and execution statistics of all SQL statements executed

  35. pg_surgery         | 1.0             |                   | extension to perform surgery on a damaged relation

  36. pg_trgm            | 1.6             |                   | text similarity measurement and index searching based on trigrams

  37. pgcrypto           | 1.3             |                   | cryptographic functions

  38. pgrowlocks         | 1.2             |                   | show row-level locking information

  39. pgstattuple        | 1.5             |                   | show tuple-level statistics

  40. pg_visibility      | 1.2             |                   | examine the visibility map (VM) and page-level visibility info

  41. postgres_fdw       | 1.1             |                   | foreign-data wrapper for remote PostgreSQL servers

  42. autoinc            | 1.0             |                   | functions for autoincrementing fields

  43. insert_username    | 1.0             |                   | functions for tracking who changed a table

  44. moddatetime        | 1.0             |                   | functions for tracking last modification time

  45. refint             | 1.0             |                   | functions for implementing referential integrity (obsolete)

  46. tablefunc          | 1.0             |                   | functions that manipulate whole tables, including crosstab

  47. tcn                | 1.0             |                   | Triggered change notifications

  48. tsm_system_rows    | 1.0             |                   | TABLESAMPLE method which accepts number of rows as a limit

  49. tsm_system_time    | 1.0             |                   | TABLESAMPLE method which accepts time in milliseconds as a limit

  50. unaccent           | 1.1             |                   | text search dictionary that removes accents

  51. sslinfo            | 1.2             |                   | information about SSL certificates

  52. xml2               | 1.1             |                   | XPath querying and XSLT

  53. bool_plperlu       | 1.0             |                   | transform between bool and plperlu

  54. bool_plperl        | 1.0             |                   | transform between bool and plperl

  55. hstore_plperl      | 1.0             |                   | transform between hstore and plperl

  56. hstore_plperlu     | 1.0             |                   | transform between hstore and plperlu

  57. jsonb_plperlu      | 1.0             |                   | transform between jsonb and plperlu

  58. jsonb_plperl       | 1.0             |                   | transform between jsonb and plperl

  59. hstore_plpythonu   | 1.0             |                   | transform between hstore and plpythonu

  60. hstore_plpython2u  | 1.0             |                   | transform between hstore and plpython2u

  61. hstore_plpython3u  | 1.0             |                   | transform between hstore and plpython3u

  62. jsonb_plpythonu    | 1.0             |                   | transform between jsonb and plpythonu

  63. jsonb_plpython2u   | 1.0             |                   | transform between jsonb and plpython2u

  64. jsonb_plpython3u   | 1.0             |                   | transform between jsonb and plpython3u

  65. ltree_plpythonu    | 1.0             |                   | transform between ltree and plpythonu

  66. ltree_plpython2u   | 1.0             |                   | transform between ltree and plpython2u

  67. ltree_plpython3u   | 1.0             |                   | transform between ltree and plpython3u

  68. (64 rows)
复制代码

可以看到postgres目前并没有可用的uuid-ossp扩展。
此时,直接创建uuid-ossp会报错,如:

postgres=# create extension "uuid-ossp";
ERROR:  could not open extension control file "/postgres/pg14/share/extension/uuid-ossp.control": No such file or directory
postgres=#

注意:
要用双引号将uuid-ossp引起来,因为有个中划线“-”。

3.PG安装uuid选项

注意:以ROOT用户去编译

su - root

cd /postgresql/soft/postgresql-14.8/
./configure --prefix=/postgresql/pg14 --with-uuid=ossp   #prefix 安装目录

该操作只是在已安装完PG后,把uuid-ossp编译安装进了PG,不影响现有库。

4.源码编译UUID

#进入扩展目录
# cd  /postgresql/soft/postgresql-14.8/contrib/uuid-ossp

#编译安装
# make && make install


5.配置uuid的软链接

--这里需要创建软连接,否则,后面创建扩展,会报错 :找不到 /postgresql/pg14/lib/libuuid.so.16

#  find / -name libuuid.so.16

/postgresql/soft/uuid/uuid-1.6.2/.libs/libuuid.so.16

# ln -s /postgresql/soft/uuid/uuid-1.6.2/.libs/libuuid.so.16 /postgresql/pg14/lib/



6.创建扩展

查看可用扩展
postgres=# select * from pg_available_extensions;
...
uuid-ossp          | 1.1             |                   | generate universally unique identifiers

可以看到已经有扩展uuid-ossp了。下面可以创建了。


创建扩展
postgres=# create extension "uuid-ossp";
CREATE EXTENSION

7.使用扩展
安装扩展成功以后,就可以使用函数uuid_generate_v4()来生产uuid了。

postgres=# select uuid_generate_v4();
           uuid_generate_v4           
--------------------------------------
9ef673d2-3652-455c-8b6e-c634165f514b

(1 row)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-6-2 02:03 , Processed in 0.099985 second(s), 19 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表