PostgreSQL

213 阅读1分钟

架构

PostgreSQL 采用 C/SClient / Server 结构。

PostgreSQL 采用多进程架构。主进程称为 Postmaster,其对应的程序名为 postgres

当接受新的客户端连接请求之后,Postmaster 会创建一个新的 postgres 服务进程,并让该服务进程专门服务新的客户端连接,直到该连接关闭为止。

管理

以下命令均以 Ubuntu 环境为例

启动

$ service postgresql start

重启

$ service postgresql restart

停止

$ service postgresql stop

状态

$ service postgresql status

可能的输出

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2022-07-12 12:22:02 CST; 1h 30min ago
    Process: 9098 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 9098 (code=exited, status=0/SUCCESS)
        CPU: 736us

Jul 12 12:22:02 shft-Vostro-5890 systemd[1]: Starting PostgreSQL RDBMS...
Jul 12 12:22:02 shft-Vostro-5890 systemd[1]: Finished PostgreSQL RDBMS.

配置

基本配置

配置文件路径

/etc/postgresql/12/main/postgresql.conf

参考:PostgreSQL: Documentation: 14: 20.3. Connections and Authentication

配置项说明默认值
listen_addresses监听服务器localhost
post监听端口5432

数据文件路径

数据文件的存放路径在 postgresql.conf 文件中配置,对应的配置项为是 data_directory。例如:

#------------------------------------------------------------------------------
# FILE LOCATIONS
#------------------------------------------------------------------------------

# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.

data_directory = '/var/lib/postgresql/12/main'		# use data in another directory
					# (change requires restart)

日志

配置项说明默认值
logging_collector日志采集开关off
log_directory日志文件路径pg_log
log_filename日志文件名postgresql-%Y-%m-%d_%H%M%S.log

psql 命令行

psqlPostgreSQL interactive terminalPostgreSQL 一个命令行交互式客户端工具

$ psql

参数

参数缩写作用示例
--host-h主机--host=127.0.0.1
--username-Upg_log--username=postgres

$ psql -h 127.0.0.1 -p 5432 -U postgres

登录的同时,使用 -d 参数指定要使用的数据库

$ psql -h 127.0.0.1 -p 5432 -U postgres -d database_test

命令

命令作用示例
\c切换数据库\c mydb
\d查看数据库的关系或关系的结构\d\d student
\l\list查看数据库

\c

切换数据库

\c 数据库名

例如

\c thingsboard

得到输出

您现在已经连接到数据库 "thingsboard", 用户 "postgres".

\l\list

查看所有的数据库

\q

退出数据库

操作

创建数据库

CREATE DATABASE mydb;
CREATE DATABASE test OWNER postgres TABLESPACE pg_default;

PostgresSQL 数据库还提供了一个 Shell 命令 createdb 来创建数据库

$ creatdb mydb;

删除数据库

DROP DATABASE test;
DROP DATABASE IF EXISTS test;