Postgres:在特定数据库中创建函数
Postgres : create function in a particular database
代码:
CREATE FUNCTION square_num(num integer)
RETURNS INTEGER AS $$
BEGIN
IF (num<0) THEN
RETURN 0;
END IF;
RETURN num*num;
END; $$
LANGUAGE PLPGSQL;
以上代码在数据库 postgres
和模式 public
中创建了一个函数。如何在特定数据库及其模式中创建函数?谢谢
The above code created a function in the database postgres and schema
public.
没有。它在 search_path
的第一个现有模式中在您连接到的数据库中创建函数。
示例:
-bash-4.2$ psql t
psql (10.1)
Type "help" for help.
t=# create table tt();
CREATE TABLE
t=# select table_catalog,table_schema from information_schema.tables where table_name = 'tt';
table_catalog | table_schema
---------------+--------------
t | public
(1 row)
我在连接上定义了数据库名称 t,因此在数据库 t 中创建了关系。我没有架构 postgres,所以 $user
被跳过,下一个 "default" 架构是 public
:
t=# show search_path;
search_path
-----------------
"$user", public
(1 row)
所以现在:
t=# create schema postgres;
CREATE SCHEMA
t=# create table tt();
CREATE TABLE
看到 - 没有关系已经存在的错误,因为:
t=# select table_catalog,table_schema from information_schema.tables where table_name = 'tt';
table_catalog | table_schema
---------------+--------------
t | public
t | postgres
(2 rows)
函数创建遵循相同的规则,我使用 table 作为较短的语法...
阅读:https://www.postgresql.org/docs/current/static/ddl-schemas.html#DDL-SCHEMAS-PATH
The first schema in the search path that exists is the default
location for creating new objects.
终于回答
How can I create a function in a particular database and it's schema?
连接到所需的数据库并明确指定模式名称:
CREATE FUNCTION my_schema.square_num(...and so on
或调整search_path
以满足您的需要
update 为了清楚起见,我使用了架构名称 postgres 以符合原始 post。使用 postgres 数据库和创建 postgres 模式可能会让新用户感到困惑。默认的 postgres 数据库(可以随时从模板重新创建)没有什么特别的(或系统的),名称 postgres 为模式提供任何特殊属性。我只将它用于 OP 以更轻松地重现该示例(从他连接到 postgres 数据库这一事实可以清楚地看出,用户可能没有指定数据库名称,以 OS 用户 身份连接postgres)。因此,为了演示 search_path
第一个值 $user
是如何被提取的,我使用了与用户名相同的名称 ...
代码:
CREATE FUNCTION square_num(num integer)
RETURNS INTEGER AS $$
BEGIN
IF (num<0) THEN
RETURN 0;
END IF;
RETURN num*num;
END; $$
LANGUAGE PLPGSQL;
以上代码在数据库 postgres
和模式 public
中创建了一个函数。如何在特定数据库及其模式中创建函数?谢谢
The above code created a function in the database postgres and schema public.
没有。它在 search_path
的第一个现有模式中在您连接到的数据库中创建函数。
示例:
-bash-4.2$ psql t
psql (10.1)
Type "help" for help.
t=# create table tt();
CREATE TABLE
t=# select table_catalog,table_schema from information_schema.tables where table_name = 'tt';
table_catalog | table_schema
---------------+--------------
t | public
(1 row)
我在连接上定义了数据库名称 t,因此在数据库 t 中创建了关系。我没有架构 postgres,所以 $user
被跳过,下一个 "default" 架构是 public
:
t=# show search_path;
search_path
-----------------
"$user", public
(1 row)
所以现在:
t=# create schema postgres;
CREATE SCHEMA
t=# create table tt();
CREATE TABLE
看到 - 没有关系已经存在的错误,因为:
t=# select table_catalog,table_schema from information_schema.tables where table_name = 'tt';
table_catalog | table_schema
---------------+--------------
t | public
t | postgres
(2 rows)
函数创建遵循相同的规则,我使用 table 作为较短的语法...
阅读:https://www.postgresql.org/docs/current/static/ddl-schemas.html#DDL-SCHEMAS-PATH
The first schema in the search path that exists is the default location for creating new objects.
终于回答
How can I create a function in a particular database and it's schema?
连接到所需的数据库并明确指定模式名称:
CREATE FUNCTION my_schema.square_num(...and so on
或调整search_path
以满足您的需要
update 为了清楚起见,我使用了架构名称 postgres 以符合原始 post。使用 postgres 数据库和创建 postgres 模式可能会让新用户感到困惑。默认的 postgres 数据库(可以随时从模板重新创建)没有什么特别的(或系统的),名称 postgres 为模式提供任何特殊属性。我只将它用于 OP 以更轻松地重现该示例(从他连接到 postgres 数据库这一事实可以清楚地看出,用户可能没有指定数据库名称,以 OS 用户 身份连接postgres)。因此,为了演示 search_path
第一个值 $user
是如何被提取的,我使用了与用户名相同的名称 ...