PostgreSQL 데이터베이스 생성, 삭제 방법

12412 단어
1. 데이터베이스 서버 설치가 완료되면 기본적으로 세 개의 데이터베이스가 있습니다. 다음 두 가지 방법으로 볼 수 있습니다.
postgres=# SELECT * FROM pg_database;
  datname  | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | dattablespace | datc
onfig |               datacl
-----------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+---------------+-----
------+-------------------------------------
 template1 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | t            |           -1 |         11563 |          648 |          1663 |
      | {=c/postgres,postgres=CTc/postgres}
 template0 |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | t             | f            |           -1 |         11563 |          648 |          1663 |
      | {=c/postgres,postgres=CTc/postgres}
 postgres  |     10 |        6 | zh_CN.UTF-8 | zh_CN.UTF-8 | f             | t            |           -1 |         11563 |          648 |          1663 |
      |
(3 rows)

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
(3 rows)

postgres=#

이 세 개의 데이터베이스는 모두 initdb에서 생성된 것이다. 그 중에서template0과template1은 데이터베이스 템플릿으로 만들 때 새로운 데이터베이스를 복제할 수 있다.
2. 만드는 방법, 어떤 사용자가 만드는지, 기본 데이터베이스 owner는 이 사용자입니다.
[postgres@kevin ~]$ psql postgres
psql (8.4.2)
Type "help" for help.

postgres=# CREATE DATABASE pg_databse_test_1;
CREATE DATABASE
postgres=# \l
                                      List of databases
       Name        |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
-------------------+----------+----------+-------------+-------------+-----------------------
 pg_databse_test_1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                     : postgres=CTc/postgres
 template1         | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                     : postgres=CTc/postgres
(4 rows)

postgres=#

다른 명령줄 생성 방법:
[postgres@kevin ~]$ createdb pg_database_test_2;
[postgres@kevin ~]$ psql
psql (8.4.2)
Type "help" for help.

postgres=# \l
                                      List of databases
        Name        |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------+----------+-------------+-------------+-----------------------
 pg_database_test_2 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                      : postgres=CTc/postgres
 template1          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                      : postgres=CTc/postgres
(5 rows)

postgres=#

3. 다른 데이터베이스 역할에 대한 데이터베이스를 만듭니다.
postgres=# \du
              List of roles
   Role name    | Attributes  | Member of
----------------+-------------+-----------
 pg_test_user_3 | Create DB   | {}
 pg_test_user_4 | Create role | {}
                : Create DB
 postgres       | Superuser   | {}
                : Create role
                : Create DB

postgres=# CREATE DATABASE pg_database_3 OWNER pg_test_user_4;
CREATE DATABASE
postgres=# \l
                                         List of databases
        Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------------+----------+-------------+-------------+-----------------------
 pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
 template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
(6 rows)

postgres=#

명령행 방법:
[postgres@kevin ~]$ createdb -O pg_test_user_3 pg_database_4;
[postgres@kevin ~]$ psql
psql (8.4.2)
Type "help" for help.

postgres=# \l
                                         List of databases
        Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------------+----------+-------------+-------------+-----------------------
 pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
 template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
(7 rows)

postgres=#

4. 템플릿 데이터베이스를 사용하여 만듭니다. 이때 템플릿에 대한 변경은 템플릿을 기반으로 만든 모든 데이터베이스 대상에 동일한 변경을 일으킵니다.
postgres=# CREATE DATABASE pg_datebase_5  TEMPLATE template0; /*SQL */
CREATE DATABASE
postgres=# \l
                                         List of databases
        Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------------+----------+-------------+-------------+-----------------------
 pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_datebase_5      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
 template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
(8 rows)

postgres=# \q
[postgres@kevin ~]$ createdb -T template0 pg_database_6 /* */
[postgres@kevin ~]$ psql
psql (8.4.2)
Type "help" for help.

postgres=# \l
                                         List of databases
        Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------------+----------+-------------+-------------+-----------------------
 pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_6      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_datebase_5      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
 template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
(9 rows)

postgres=#

5. 데이터베이스를 삭제합니다.
SQL 방법으로 제거:
postgres=# \l
                                         List of databases
        Name        |     Owner      | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------------+----------+-------------+-------------+-----------------------
 pg_database_3      | pg_test_user_4 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_4      | pg_test_user_3 | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_6      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_7      | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_database_test_2 | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
 template1          | postgres       | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                            : postgres=CTc/postgres
(9 rows)

postgres=# DROP DATABASE pg_database_3;
DROP DATABASE
postgres=# DROP DATABASE pg_database_4;
DROP DATABASE
postgres=# DROP DATABASE pg_database_6;
DROP DATABASE
postgres=# DROP DATABASE pg_database_7;
DROP DATABASE
postgres=# \l
                                      List of databases
        Name        |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
--------------------+----------+----------+-------------+-------------+-----------------------
 pg_database_test_2 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 pg_databse_test_1  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 postgres           | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                      : postgres=CTc/postgres
 template1          | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                                      : postgres=CTc/postgres
(5 rows)

postgres=#

명령줄로 삭제하려면 다음과 같이 하십시오.
postgres=# \q
[postgres@kevin ~]$ dropdb pg_database_test_2
[postgres@kevin ~]$ dropdb pg_databse_test_1
[postgres@kevin ~]$ psql
psql (8.4.2)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
                                                             : postgres=CTc/postgres
(3 rows)

postgres=#

좋은 웹페이지 즐겨찾기