什么是有效的 PostgreSQL 数据库名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10216784/
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-10 23:27:14  来源:igfitidea点击:

What is a valid PostgreSQL database name?

postgresql

提问by dan

I create a database with a hyphen in the middle of the name with createdb. That successfully creates the database, but within the psqlinteractive client, I get a syntax error if I try a command like this:

我创建了一个数据库,名称中间有一个连字符createdb。这成功创建了数据库,但在psql交互式客户端中,如果我尝试这样的命令,我会收到语法错误:

ALTER DATABASE my-database SET SCHEMA = myschema,public;

ALTER DATABASE my-database SET SCHEMA = myschema,public;

psqlcomplains of a syntax error at or near "-"

psql抱怨“-”处或附近的语法错误

Is there some documentation for what counts as a valid PostgreSQL database name?

是否有一些文档说明什么是有效的 PostgreSQL 数据库名称?

Should I just underscores instead of hyphens?

我应该只用下划线而不是连字符吗?

回答by kgrittn

The documentation you asked about is here:

您询问的文档在这里:

http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Most people just stick to lowercase letters, numeric digits, and underscores -- to avoid typing the quotes all the time.

大多数人只是坚持使用小写字母、数字和下划线——以避免一直输入引号。

回答by Adam M.

Try putting it in double quotes:

尝试将其放在双引号中:

ALTER DATABASE "my-database" SET SCHEMA = myschema,public;

ALTER DATABASE "my-database" SET SCHEMA = myschema,public;

回答by Saurabh Sinha

I faced one issue and above answers helped me. So sharing scenario on dbname

我遇到了一个问题,以上答案对我有所帮助。所以在dbname上分享场景

Scenario:I was tried to change database name using PG admin III. My database name was My_Database

场景:我试图使用 PG admin III 更改数据库名称。我的数据库名称是 My_Database

running below queries failed:

运行以下查询失败:

ALTER DATABASE My_Database RENAME TO dba;
ALTER DATABASE [My_Database] RENAME TO dba;

ALTER DATABASE 'My_Database' RENAME TO dba;

Then i tried below and its successful

然后我在下面尝试并成功

ALTER DATABASE "My_Database" RENAME TO dba;