1

我正在将数据库从 SQL Server 移植到 Postgresql。我正在寻找user_id()SQL Server 中该函数的等效项,但从postgresql docs看来,没有等效项。

current_user它的别名user对我没有用,因为它们返回名称(文本),而我想要的是用户的 ID(在 SQL Server 中返回)。

任何有关如何在 PG 中复制这一重要功能的帮助将不胜感激。

4

2 回答 2

2

The userid is stored in the pg_user table. Using current_user, you can select the userid. You could write a procedure to wrap this up for you, I'd imagine this is pretty much what the user_id() function is doing behind the scenes, but I'm not aware of an out-of-the-box function that provides it for you already:

select usesysid from pg_user where usename = CURRENT_USER;

Here is documentation on the pg_user table: http://www.postgresql.org/docs/current/static/view-pg-user.html

于 2013-11-06T23:23:51.590 回答
2

I don't think there's a built-in way to get it, but you could certainly make a function:

CREATE OR REPLACE FUNCTION user_id() RETURNS INT AS $$
    SELECT usesysid::int 
    FROM pg_user
    WHERE usename = CURRENT_USER
$$
    LANGUAGE SQL
;
于 2013-11-06T23:25:57.140 回答