在Postgres中为插入语句生成UUID吗? [英] Generating a UUID in Postgres for Insert statement?

查看:116
本文介绍了在Postgres中为插入语句生成UUID吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单.我知道UUID的概念,我想生成一个引用UDB的存储"中的每个项目".似乎合理吗?

My question is rather simple. I'm aware of the concept of a UUID and I want to generate one to refer to each 'item' from a 'store' in my DB with. Seems reasonable right?

问题是以下行返回错误:

The problem is the following line returns an error:

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

I've read the page at: http://www.postgresql.org/docs/current/static/uuid-ossp.html

我正在Ubuntu 10.04 x64上运行Postgres 8.4.

I'm running Postgres 8.4 on Ubuntu 10.04 x64.

推荐答案

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

uuid-ossp is a contrib module, so it isn't loaded into the server by default. You must load it into your database to use it.

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

For modern PostgreSQL versions (9.1 and newer) that's easy:

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

,但对于9.0及更低版本,您必须改为运行SQL脚本来加载扩展.请参见关于8.4中的contrib模块的文档.

but for 9.0 and below you must instead run the SQL script to load the extension. See the documentation for contrib modules in 8.4.

对于Pg 9.1及更高版本,请阅读当前的贡献文档 CREATE EXTENSION .这些功能在9.0或更早的版本(例如8.4)中不存在.

For Pg 9.1 and newer instead read the current contrib docs and CREATE EXTENSION. These features do not exist in 9.0 or older versions, like your 8.4.

如果您使用的是PostgreSQL的打包版本,则可能需要安装一个单独的包含contrib模块和扩展名的软件包.在软件包管理器数据库中搜索"postgres"和"contrib".

If you're using a packaged version of PostgreSQL you might need to install a separate package containing the contrib modules and extensions. Search your package manager database for 'postgres' and 'contrib'.

这篇关于在Postgres中为插入语句生成UUID吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆