我的数据库在哪里用postgres?

时间:2013-09-05 02:55:45

标签: postgresql-9.2

postgres拒绝工作。我正在使用9.2和一个新手。

我创建了一个数据库。我列出了它不在那里?没有错误!它去了哪里?有没有创造过?

postgres-# creatdb test
postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | 
 template0 | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres-# 



postgres@ubuntu:/home/ubuntu$ psql -h 127.0.0.1 -U postgres -d test
Password for user postgres: 
psql: FATAL:  database "test" does not exist

2 个答案:

答案 0 :(得分:24)

您有两个错误:

  1. createdb操作系统命令,它不是SQL命令。在psql中,您需要使用SQL语句,即:CREATE DATABASE。有关详细信息,请参阅手册:http://www.postgresql.org/docs/current/static/sql-createdatabase.html

  2. 每个SQL语句都需要以;终止。由于您没有这样做,您的声明未执行,因此您没有收到错误。有关详细信息,请参阅手册:http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html

  3. postgres=# createdb test;
    ERROR:  syntax error at or near "createdb"
    LINE 1: createdb test;
            ^
    postgres=# create database test;
    CREATE DATABASE
    postgres=# \list
                                              List of databases
       Name    |  Owner   | Encoding |       Collate       |        Ctype        |   Access privileges
    -----------+----------+----------+---------------------+---------------------+-----------------------
     postgres  | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | 
     template0 | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | =c/postgres          +
               |          |          |                     |                     | postgres=CTc/postgres
     template1 | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | postgres=CTc/postgres+
               |          |          |                     |                     | =c/postgres
     test      | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 |
     10 rows)
    
    
    postgres=#
    

答案 1 :(得分:1)

createdb是一个shell命令而不是postgres命令,这就是为什么它没有显示任何错误或成功消息。

要从终端创建数据库,您必须这样做。

sudo -u postgres createdb databaseName

要从postgres命令创建数据库,您必须一个接一个地运行以下命令

sudo -u postgres psql
CREATE DATABASE testDB;

如果数据库创建成功,那么您将获得成功消息" CREATE DATABASE"

enter image description here

要查看数据库列表,请编写此命令

\l