Postgresql 没有使用“createdb”作为超级用户创建数据库,但没有输出错误

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

Postgresql not creating db with “createdb” as superuser, yet not outputting errors

postgresqlpsql

提问by Damien Roche

I am working with a fresh postgresql install, with 'postgres' super user. Logged in via:

我正在使用“postgres”超级用户进行全新的 postgresql 安装。登录方式:

sudo -u postgres psql


postgres=# createdb database
postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | 
 template0 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 template1 | postgres | UTF8     | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres

No errors, yet table is not being created. Any ideas?

没有错误,但尚未创建表。有任何想法吗?

回答by Saddam Abu Ghaida

createdbis a command line utility which you can run from bashand not from psql. To create a database from psql, use the create databasestatement like so:

createdb是一个命令行实用程序,您可以从bash而不是从 psql运行它。要从psql创建数据库,请使用如下create database语句:

create database [databasename];

Note: be sure to always end your SQL statements with ;

注意:请务必始终以 ;

回答by a_horse_with_no_name

Late to the party, but the accepted answer doesn't explain why no error is displayed. And as this is something Postgres newcomers often stumble upon, I wanted to add that.

迟到了,但接受的答案并没有解释为什么没有显示错误。因为这是 Postgres 新手经常偶然发现的东西,所以我想补充一下。



TL/TR: always end your SQL statements with ;

TL/TR:始终以 SQL 语句结束 ;



Because the createdb databasedid not end with ;psqlthinks the statement isn't finished and waits for more input. This is indicated by the prompt changing from postgres=#to postgres-#. An extremely subtle change that I wish psqlwould do differently (more "prominent").

因为createdb database没有结束;psql认为语句没有完成并等待更多输入。这由从postgres=#变为的提示指示postgres-#。我希望一个非常微妙的变化psql会有所不同(更“突出”)。

By entering the meta-command \listthe "current" SQL statement is "aborted" without executing it.

通过输入元命令\list,“当前”SQL 语句被“中止”而不执行。

If the createdbhad been ended with a ;the output would have been:

如果createdb以 a 结尾,;则输出将是:

postgres=> createdb foobar;
ERROR:  syntax error at or near "createdb"
LINE 1: createdb foobar;
        ^
postgres=>

Clearly showing that something was wrong.

清楚地表明出了什么问题。

回答by Fredrick Mgbeoma

I was in this situation not long ago. In case someone else experiences this, considering that the command prompt shows postgres-#you can execute the pending createdb command by simply typing ;and the return key.

我不久前就处于这种情况。如果其他人遇到这种情况,考虑到命令提示符显示postgres-#您可以通过简单地输入;和回车键来执行挂起的 createdb 命令。

回答by Avtandil Kavrelishvili

Create new data base in PostgreSQL is very simple, execute this command on Linux (CentOS 7 example):

在 PostgreSQL 中创建新数据库非常简单,在 Linux 上执行这个命令(CentOS 7 示例):

sudo -u postgres psql -c "create database MyDb;"