数据库管理系统(DBMS),用于管理数据库的软件系统。常见的关系型 DBMS 有 Oracle、MySQL、SQL Server、PostgreSQL、Db2、SQLite 等等。常见的 NoSQL DBMS 有 Redis、MongoDB、Cassandra、Neo4j 等等。PostgreSQL 荣获了数据库排名网站 DB-Engines 的 2017、2018 以及 2020 年度数据库管理系统称号。
实例(Instance),由 PostgreSQL 后台进程和相关的内存组成。启动服务器进程时创建一个实例,关闭服务器进程时实例随之关闭。启动 PostgreSQL 服务器进程之后,可以通过操作系统的ps命令查询相关的后台进程:
[root@centos7 ~]# ps -ef | grep postgres | grep -v 'grep'
postgres 28003 1 0 Jan04 ? 00:00:01 /usr/pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/
postgres 28011 28003 0 Jan04 ? 00:00:00 postgres: logger
postgres 28013 28003 0 Jan04 ? 00:00:00 postgres: checkpointer
postgres 28014 28003 0 Jan04 ? 00:00:01 postgres: background writer
postgres 28015 28003 0 Jan04 ? 00:00:01 postgres: walwriter
postgres 28016 28003 0 Jan04 ? 00:00:01 postgres: autovacuum launcher
postgres 28017 28003 0 Jan04 ? 00:00:02 postgres: stats collector
postgres 28018 28003 0 Jan04 ? 00:00:00 postgres: logical replication launcher
数据库集群,每个 PostgreSQL 实例管理的都是一个数据库集群,它可以包含多个数据库。需要注意,这里的集群不是多台服务器组成的集群。
数据库(Database),一个数据库由一组相关的对象组成,例如表、索引、视图、存储过程等等。
[root@centos7 data]# su - postgres
Last login: Sat Jan 5 16:57:07 EST 2019 on pts/3
-bash-4.2$ psql
psql (11.1)
Type "help" for help.
postgres=#
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
(3 rows)
postgres=# SELECT datname FROM pg_database;
datname
-----------
postgres
template1
template0
(3 rows)
CREATE DATABASE name;
postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
testdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
ALTER DATABASE name action;
postgres=# ALTER DATABASE testdb RENAME TO newdb;
ALTER DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
newdb | 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=# ALTER DATABASE newdb OWNER TO tony;
ALTER DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
newdb | tony | 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)
ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE newdb SET enable_indexscan TO off;
ALTER DATABASE newdb RESET enable_indexscan;
DROP DATABASE [ IF EXISTS ] name
postgres=# DROP DATABASE newdb;
DROP DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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
(3 rows)
postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \c testdb;
You are now connected to database "testdb" as user "postgres".
testdb=# \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
testdb=# SELECT nspname FROM pg_namespace;
nspname
--------------------
pg_toast
pg_temp_1
pg_toast_temp_1
pg_catalog
public
information_schema
(6 rows)
CREATE SCHEMA,创建一个新的模式。
ALTER SCHEMA,修改模式的属性。
DROP SCHEMA,删除一个模式。
testdb=# CREATE SCHEMA hr;
CREATE SCHEMA
testdb=# \dn
List of schemas
Name | Owner
--------+----------
hr | postgres
public | postgres
(2 rows)
testdb=# CREATE SCHEMA app AUTHORIZATION tony;
CREATE SCHEMA
testdb=# \dn
List of schemas
Name | Owner
--------+----------
app | tony
hr | postgres
public | postgres
(3 rows)
ALTER SCHEMA name RENAME TO new_name
ALTER SCHEMA name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
testdb=# ALTER SCHEMA hr OWNER TO tony;
ALTER SCHEMA
testdb=# \dn
List of schemas
Name | Owner
--------+----------
app | tony
hr | tony
public | postgres
(3 rows)
DROP SCHEMA name;
testdb=# DROP SCHEMA hr;
DROP SCHEMA
testdb=# \dn
List of schemas
Name | Owner
--------+----------
app | tony
public | postgres
(2 rows)
DROP SCHEMA name CASCADE;
允许多个用户使用同一个数据库而不会互相干扰,他们可以使用不同的模式来维护自己的数据。
将数据库对象进行逻辑上的分组,便于管理。
第三方应用可以使用单独的模式,不会与系统中的其他对象产生命名冲突。
原文作者:SQL编程思想;如有侵权,请联系管理员删除。