什么是有效的 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
What is a valid PostgreSQL database name?
提问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 psql
interactive 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;
psql
complains 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;