gpt4 book ai didi

postgresql - 在 Postgres 中为插入语句生成 UUID?

转载 作者:行者123 更新时间:2023-11-29 11:03:54 25 4
gpt4 key购买 nike

我的问题很简单。我知道 UUID 的概念,我想生成一个 UUID 来引用我数据库中“商店”中的每个“项目”。看起来很合理吧?

问题是以下行返回错误:

honeydb=# insert into items values(
uuid_generate_v4(), 54.321, 31, 'desc 1', 31.94);
ERROR: function uuid_generate_v4() does not exist
LINE 2: uuid_generate_v4(), 54.321, 31, 'desc 1', 31.94);
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

我已阅读页面:http://www.postgresql.org/docs/current/static/uuid-ossp.html

我在 Ubuntu 10.04 x64 上运行 Postgres 8.4。

最佳答案

uuid-ossp 是一个 contrib 模块,因此默认情况下它不会加载到服务器中。您必须将其加载到数据库中才能使用。

对于现代 PostgreSQL 版本(9.1 和更新版本),这很容易:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

但对于 9.0 及以下版本,您必须改为运行 SQL 脚本来加载扩展。参见 the documentation for contrib modules in 8.4 .

对于 Pg 9.1 和更新版本,请阅读 the current contrib docsCREATE EXTENSION .这些功能在 9.0 或更早的版本中不存在,例如您的 8.4。

如果您使用的是打包版本的 PostgreSQL,您可能需要安装一个单独的包,其中包含贡献模块和扩展。在您的包管理器数据库中搜索“postgres”和“contrib”。

关于postgresql - 在 Postgres 中为插入语句生成 UUID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com