PostgreSQL数据库创建、删除方法


1.在PostgreSQL数据库服务器安装完成后,默认有三个数据库,可以通过下面两种方法查看。

  1. postgres=# SELECT * FROM pg_database;  
  2.   datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datc  
  3. onfig |               datacl  
  4. -----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+---------------+-----  
  5. ------+-------------------------------------  
  6.  template1 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | t            |           -1 |         11563 |          648 |          1663 |  
  7.       | {=c/postgres,postgres=CTc/postgres}  
  8.  template0 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | f            |           -1 |         11563 |          648 |          1663 |  
  9.       | {=c/postgres,postgres=CTc/postgres}  
  10.  postgres  |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | f             | t            |           -1 |         11563 |          648 |          1663 |  
  11.       |  
  12. (3 rows)  
  13.   
  14. postgres=# \l  
  15.                                   List of databases  
  16.    Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges  
  17. -----------+----------+----------+-------------+-------------+-----------------------  
  18.  postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  19.  template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  20.                                                              : postgres=CTc/postgres  
  21.  template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  22.                                                              : postgres=CTc/postgres  
  23. (3 rows)  
  24.   
  25. postgres=#  

这三个数据库均是由initdb生成的,其中template0 和template1 为数据库模板,创建时直接可以使用其克隆一个新数据库。

2. 创建方法,由什么用户创建,默认数据库owner就为此用户。

  1. [postgres@kevin ~]$ psql postgres  
  2. psql (8.4.2)  
  3. Type "help" for help.  
  4.   
  5. postgres=# CREATE DATABASE pg_databse_test_1;  
  6. CREATE DATABASE  
  7. postgres=# \l  
  8.                                       List of databases  
  9.        Name        |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges  
  10. -------------------+----------+----------+-------------+-------------+-----------------------  
  11.  pg_databse_test_1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  12.  postgres          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  13.  template0         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  14.                                                                      : postgres=CTc/postgres  
  15.  template1         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  16.                                                                      : postgres=CTc/postgres  
  17. (4 rows)  
  18.   
  19. postgres=#  

另一种命令行创建方法:

  1. [postgres@kevin ~]$ createdb pg_database_test_2;  
  2. [postgres@kevin ~]$ psql  
  3. psql (8.4.2)  
  4. Type "help" for help.  
  5.   
  6. postgres=# \l  
  7.                                       List of databases  
  8.         Name        |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges  
  9. --------------------+----------+----------+-------------+-------------+-----------------------  
  10.  pg_database_test_2 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  11.  pg_databse_test_1  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  12.  postgres           | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  13.  template0          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  14.                                                                       : postgres=CTc/postgres  
  15.  template1          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  16.                                                                       : postgres=CTc/postgres  
  17. (5 rows)  
  18.   
  19. postgres=#  

3.为其他数据库角色创建数据库。

  1. postgres=# \du  
  2.               List of roles  
  3.    Role name    | Attributes  | Member of  
  4. ----------------+-------------+-----------  
  5.  pg_test_user_3 | Create DB   | {}  
  6.  pg_test_user_4 | Create role | {}  
  7.                 : Create DB  
  8.  postgres       | Superuser   | {}  
  9.                 : Create role  
  10.                 : Create DB  
  11.   
  12. postgres=# CREATE DATABASE pg_database_3 OWNER pg_test_user_4;  
  13. CREATE DATABASE  
  14. postgres=# \l  
  15.                                          List of databases  
  16.         Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges  
  17. --------------------+----------------+----------+-------------+-------------+-----------------------  
  18.  pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  19.  pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  20.  pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  21.  postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  22.  template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  23.                                                                             : postgres=CTc/postgres  
  24.  template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  25.                                                                             : postgres=CTc/postgres  
  26. (6 rows)  
  27.   
  28. postgres=#  

命令行方法:

  1. [postgres@kevin ~]$ createdb -O pg_test_user_3 pg_database_4;  
  2. [postgres@kevin ~]$ psql  
  3. psql (8.4.2)  
  4. Type "help" for help.  
  5.   
  6. postgres=# \l  
  7.                                          List of databases  
  8.         Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges  
  9. --------------------+----------------+----------+-------------+-------------+-----------------------  
  10.  pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  11.  pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  12.  pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  13.  pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  14.  postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  15.  template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  16.                                                                             : postgres=CTc/postgres  
  17.  template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  18.                                                                             : postgres=CTc/postgres  
  19. (7 rows)  
  20.   
  21. postgres=#  

4. 使用模板数据库创建,此时,对模板的更改会引起所有基于它创建的数据库对象发生相同的变更。

  1. postgres=# CREATE DATABASE pg_datebase_5  TEMPLATE template0; /*SQL方式*/  
  2. CREATE DATABASE  
  3. postgres=# \l  
  4.                                          List of databases  
  5.         Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges  
  6. --------------------+----------------+----------+-------------+-------------+-----------------------  
  7.  pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  8.  pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  9.  pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  10.  pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  11.  pg_datebase_5      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  12.  postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  13.  template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  14.                                                                             : postgres=CTc/postgres  
  15.  template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  16.                                                                             : postgres=CTc/postgres  
  17. (8 rows)  
  18.   
  19. postgres=# \q  
  20. [postgres@kevin ~]$ createdb -T template0 pg_database_6 /*命令行方式*/  
  21. [postgres@kevin ~]$ psql  
  22. psql (8.4.2)  
  23. Type "help" for help.  
  24.   
  25. postgres=# \l  
  26.                                          List of databases  
  27.         Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges  
  28. --------------------+----------------+----------+-------------+-------------+-----------------------  
  29.  pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  30.  pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  31.  pg_database_6      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  32.  pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  33.  pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  34.  pg_datebase_5      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  35.  postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |  
  36.  template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  37.                                                                             : postgres=CTc/postgres  
  38.  template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres  
  39.                                                                             : postgres=CTc/postgres  
  40. (9 rows)  
  41.   
  42. postgres=#  
  • 1
  • 2
  • 下一页

相关内容