27.6. 常用命令

下面列出的命令是我们常用的命令,但还有更多命令。请查阅 man 手册以获取更多详细信息。要在数据库中定义新用户,请运行 createuser 实用程序
                 [root@deep] /# su postgres
                 [postgres@deep /]$ createuser
               

                 Enter name of user to add ---> admin
                 Enter user's postgres ID or RETURN to use unix user ID: 500 ->
                 Is user "admin" allowed to create databases (y/n) y
                 Is user "admin" a superuser? (y/n) y
                 createuser: admin was successfully added
               

要在数据库中删除用户,请运行 destroyuser 实用程序
                 [root@deep] /# su postgres
                 [postgres@deep /]$ destroyuser
               

                 Enter name of user to delete ---> admin
                 destroyuser: delete of user admin was successful.
               

要创建新数据库,请运行 createdb 实用程序
                 [root@deep] /# su postgres
                 [postgres@deep /]$ createdb dbname  (1)
               

(1)
dbname 是数据库的名称。

或者使用 Postgres 终端监控程序 (psql)
                 [root@deep] /# su admin
                 [admin@deep /]$ psql template1
               

                 Welcome to the POSTGRESQL interactive sql monitor:
                 Please read the file COPYRIGHT for copyright terms of POSTGRESQL
                 [PostgreSQL 6.5.3 on i686-pc-linux-gnu, compiled by egcs ]

                 type \? for help on slash commands
                 type \q to quit
                 type \g or terminate with semicolon to execute query
                 You are currently connected to the database: template1
               
                 template1-> create database foo;
                 CREATEDB
               

注意: 客户端连接可以通过 IP 地址和/或用户名进行限制,通过pg_hba.conf文件位于PG_DATA.

其他有用的 Postgres 终端监控程序命令 (psql) 包括:要连接到新数据库,请使用以下命令
                 template1-> \c foo
               
正在连接到新数据库:foo
                 foo->
               

要创建表,请使用以下命令
                 foo-> create table bar (i int4, c char(16));
                 CREATE
                 foo->
               

要检查新表,请使用以下命令
                 foo-> \d bar
               
                 foo->
               
                 Table    = bar
                 +----------------------------------+----------------------------------+------------+
                 |              Field	|          Type	        |                          Length  |
                 +----------------------------------+----------------------------------+------------+
                 | I	                | int4	                |                                4 |
                 | c	                | char()	        |                               16 |
                 +----------------------------------+----------------------------------+------------+
               

要删除表、索引、视图,请使用以下命令
                 foo-> drop table table_name;
                 foo-> drop index index_name;
                 foo-> drop view view_name;
               

要插入数据:一旦创建表,就可以使用以下命令填充数据
                 foo-> insert into table_name (name_of_attr1, name_of_attr2, name_of_attr3)
                 foo-> values (value1, value2, value3);