PostgreSQL 操作数据库

一、创建数据库

PostgreSQL 创建数据库基于环境不同,有以下几种创建方法:

  • 使用 CREATE DATABASE SQL语句来创建;
  • 使用 createdb 命令来创建;
  • 使用 pgAdmin 工具来创建;

1.1 使用CREATE DATABASE创建数据库

CREATE DATABASE 命令需要在PostgreSQL交互命令窗口执行,语法格式如下:

CREATE DATABASE <dbname>;

创建数据库mytest_db,命令如下:

[root@localhost ~]# su - postgres
Last login: Mon Nov 18 15:21:52 CST 2019 on pts/1
-bash-4.2$ psql
psql (10.10)
Type "help" for help.

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

使用\l可以查询到刚创建的数据库mytest_db

2. 使用 createdb 命令创建数据库

createdbPostgreSQLCREATE DATABASE SQL语句的封装,取名为createdb。其使用方法为:

createdb [option ...] [dbname [description]]

参数说明:
dbname:要创建的数据库名。
description:关于新创建的数据库相关的说明。
options:参数可选项,可以使用 --help得到:

[root@localhost ~]# /usr/pgsql-10/bin/createdb --help
createdb creates a PostgreSQL database.

Usage:
  createdb [OPTION]... [DBNAME] [DESCRIPTION]

Options:
  -D, --tablespace=TABLESPACE  default tablespace for the database
  -e, --echo                   show the commands being sent to the server
  -E, --encoding=ENCODING      encoding for the database
  -l, --locale=LOCALE          locale settings for the database
      --lc-collate=LOCALE      LC_COLLATE setting for the database
      --lc-ctype=LOCALE        LC_CTYPE setting for the database
  -O, --owner=OWNER            database user to own the new database
  -T, --template=TEMPLATE      template database to copy
  -V, --version                output version information, then exit
  -?, --help                   show this help, then exit

Connection options:
  -h, --host=HOSTNAME          database server host or socket directory
  -p, --port=PORT              database server port
  -U, --username=USERNAME      user name to connect as
  -w, --no-password            never prompt for password
  -W, --password               force password prompt
  --maintenance-db=DBNAME      alternate maintenance database

By default, a database with the same name as the current user is created.

Report bugs to <[email protected]>.

创建数据库mytest2_db,命令如下:

[root@localhost ~]# /usr/pgsql-10/bin/createdb mytest2_db -U postgres
[root@localhost ~]# su - postgres
Last login: Mon Nov 18 16:20:33 CST 2019 on pts/1
-bash-4.2$ psql
psql (10.10)
Type "help" for help.

postgres=# \l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
------------+----------+----------+-------------+-------------+-----------------------
 mytest2_db | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 mytest_db  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
(5 rows)

postgres=#

可以看到mytest2_db已经创建好了。

3. 使用pgAdmin工具创建数据库

二、查询数据库

  1. 使用\l命令查看已经创建好的数据库,上面已经有例子。接着可以使用\c <dbname>来进入数据库,如下:
postgres=# \l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
------------+----------+----------+-------------+-------------+-----------------------
 mytest2_db | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 mytest_db  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
(5 rows)

postgres=# \c mytest_db
You are now connected to database "mytest_db" as user "postgres".
mytest_db=#

  1. 直接在终端连接数据库并进入数据库,命令为:
[root@localhost ~]# /usr/pgsql-10/bin/psql mytest_db -U postgres
psql (10.10)
Type "help" for help.

mytest_db=#

三、删除数据库

同样与创建数据库对应,删除数据库同样有以下几种方式:

  • 使用 DROP DATABASE SQL语句来删除;
  • 使用 dropdb 命令来删除;
  • 使用 pgAdmin 工具来删除;

3.1 DROP DATABASE 删除数据库

DROP DATABASE 会删除数据库的系统目录项并且删除包含数据的文件目录。
DROP DATABASE 只能由超级管理员或数据库拥有者执行。
DROP DATABASE 命令需要在 PostgreSQL 命令窗口来执行,语法格式如下:

DROP DATABASE [ IF EXISTS ] name

参数说明:

  • IF EXISTS:如果数据库不存在则发出提示信息,而不是错误信息,类似于python的try…except…程序结构。
  • name:要删除的数据库的名称。

例如,将mytest_db数据库删除:

postgres=# DROP DATABASE IF EXISTS mytest;
NOTICE:  database "mytest" does not exist, skipping
DROP DATABASE
postgres=# DROP DATABASE mytest;
ERROR:  database "mytest" does not exist
postgres=# DROP DATABASE IF EXISTS mytest_db;
DROP DATABASE
postgres=# \l
                                  List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
------------+----------+----------+-------------+-------------+-----------------------
 mytest2_db | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
(4 rows)

postgres=#

使用\l查看下,可以看到mytest_db数据库已经被删除了。

3.2 dropdb 命令删除数据库

dropdbPostgreSQLDROP DATABASE SQL语句的封装,取名为dropdb命令。
dropdb 用于删除 PostgreSQL 数据库。
dropdb 命令只能由超级管理员或数据库拥有者执行。
dropdb 命令语法格式如下:

dropdb [connection-option...] [option...] dbname

参数说明:
dbname:要删除的数据库名。
options:参数可选项,可以是以下值:

[root@localhost ~]# /usr/pgsql-10/bin/dropdb --help
dropdb removes a PostgreSQL database.

Usage:
  dropdb [OPTION]... DBNAME

Options:
  -e, --echo                show the commands being sent to the server
  -i, --interactive         prompt before deleting anything
  -V, --version             output version information, then exit
  --if-exists               don't report error if database doesn't exist
  -?, --help                show this help, then exit

Connection options:
  -h, --host=HOSTNAME       database server host or socket directory
  -p, --port=PORT           database server port
  -U, --username=USERNAME   user name to connect as
  -w, --no-password         never prompt for password
  -W, --password            force password prompt
  --maintenance-db=DBNAME   alternate maintenance database

Report bugs to <[email protected]>.

例如,将mytest2_db数据库删除:

[root@localhost ~]# /usr/pgsql-10/bin/dropdb mytest2_db -U postgres
[root@localhost ~]# /usr/pgsql-10/bin/psql mytest2_db -U postgres
psql: FATAL:  database "mytest2_db" does not exist

先使用dropdb命令删除数据mytest2_db,然后使用psql查看下该数据库,发现其已经不存在了,被删除了。

Guess you like

Origin blog.csdn.net/Zhaopanp_Crise/article/details/103124581