postgresql 扩展存在但 uuid_generate_v4 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22446478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:39:14  来源:igfitidea点击:

Extension exists but uuid_generate_v4 fails

postgresqlamazon-web-servicesamazon-ec2

提问by アレックス

At amazon ec2 RDS Postgresql:

在亚马逊 ec2 RDS Postgresql:

=> SHOW rds.extensions;

rds.extensions                                                                                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 btree_gin,btree_gist,chkpass,citext,cube,dblink,dict_int,dict_xsyn,earthdistance,fuzzystrmatch,hstore,intagg,intarray,isn,ltree,pgcrypto,pgrowlocks,pg_trgm,plperl,plpgsql,pltcl,postgis,postgis_tiger_geocoder,postgis_topology,sslinfo,tablefunc,tsearch2,unaccent,uuid-ossp
(1 row)

As you can see, uuid-osspextension does exist. However, when I'm calling the function for generation uuid_v4, it fails:

如您所见,uuid-ossp扩展确实存在。但是,当我调用 generation 函数时uuid_v4,它失败了:

CREATE TABLE my_table (
    id uuid DEFAULT uuid_generate_v4() NOT NULL,
    name character varying(32) NOT NULL,

);

What's wrong with this?

这有什么问题?

回答by Craig Ringer

The extension is availablebut not installedin this database.

扩展可用,但未安装在此数据库中。

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

回答by atomCode

If the extension is already there but you don't see the uuid_generate_v4() function when you do a describe functions \dfcommand then all you need to do is drop the extension and re-add it so that the functions are also added. Here is the issue replication:

如果扩展已经存在,但在执行 describe functions \df命令时没有看到 uuid_generate_v4() 函数,那么您需要做的就是删除扩展并重新添加它,以便也添加函数。这是问题复制:

db=# \df
                       List of functions
 Schema | Name | Result data type | Argument data types | Type
--------+------+------------------+---------------------+------
(0 rows)
CREATE EXTENSION "uuid-ossp";
ERROR:  extension "uuid-ossp" already exists
DROP EXTENSION "uuid-ossp";
CREATE EXTENSION "uuid-ossp";
db=# \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

db=# select uuid_generate_v4();
           uuid_generate_v4
--------------------------------------
 b19d597c-8f54-41ba-ba73-02299c1adf92
(1 row)

What probably happened is that the extension was originally added to the cluster at some point in the past and then you probably created a new database within that cluster afterward. If that was the case then the new database will only be "aware" of the extension but it will not have the uuid functions added which happens when you add the extension. Therefore you must re-add it.

可能发生的情况是,扩展最初是在过去的某个时间点添加到集群中的,之后您可能在该集群中创建了一个新数据库。如果是这种情况,那么新数据库将只“知道”扩展名,但不会添加添加扩展名时发生的 uuid 函数。因此,您必须重新添加它。

回答by Olalekan Sogunle

Looks like the extension is not installed in the particular database you require it.

看起来扩展没有安装在您需要的特定数据库中。

You should connect to this particular database with

你应该连接到这个特定的数据库

 \CONNECT my_database

Then install the extension in this database

然后在这个数据库中安装扩展

 CREATE EXTENSION "uuid-ossp";

回答by Miguel Becerra

This worked for me.

这对我有用。

create extension IF NOT EXISTS "uuid-ossp" schema pg_catalog version "1.1"; 

make sure the extension should by on pg_catalog and not in your schema...

确保扩展应该在 pg_catalog 上而不是在你的架构中......

回答by odin38

if you do it from unix command (apart from PGAdmin) dont forget to pass the DB as a parameter. otherwise this extension will not be enabled when executing requests on this DB

如果您从 unix 命令(除了 PGAdmin)执行此操作,请不要忘记将 DB 作为参数传递。否则在此数据库上执行请求时将不会启用此扩展

psql -d -c "create EXTENSION pgcrypto;"

psql -d -c "创建扩展 pgcrypto;"