Skip to main content

关系型数据库服务器-PostgreSQL

关系型数据库服务器-PostgreSQL

概述

PostgreSQL是自由的对象-关系型数据库服务器(数据库管理系统),PostgreSQL支持大部分SQL标准并且提供了许多其他现代特性:复杂查询、外键、触发器、视图、事务完整性、MVCC。

安装过程

102以root用户执行如下命令,安装postgresql-server包。

yum install postgresql-server

103执行如下命令,初始化数据库。

postgresql-setup --initdb

104执行如下命令,启动postgresql服务。

systemctl start postgresql

105执行如下命令,查看postgresql服务状态。

systemctl status postgresql

Active:active (running) since Sat 2020-05-23 04:46:14 EDT;2s ago Process: 48102 Exetartre=/usr/libexc/postgresq-hek-d-dir postgresql(code=exited,status=0/SUCCESS) Main PID:48104 (postmaster) Tasks:8(Limit:11337) Memory: 16.3M CGroup:/system.sLice/postgresql.service 48104/usr/bin/postmaster -D /var/Lib/pgsql/data -48106 postgres:logger process -48108 postgres: checkpointer process -48109 postgres:writer process -48110 postgres:wal writer process -48111postgres:autovacuum Launcher process -48112 postgres: stats collector process 48113 postgres: bgworker: Logical replication Launcher May2304:46:14 Localhost, Localdomain systemd[1]:Starting PostgreSQL database server.. May 23 04:46:14localhost, Localdomain postmaster 48104):2020-05-23 04:46:14.581EDT[48104] LOG:Listening on IP6adress ":", port 543 May 23 04:46:14 localhost. caldomain postmaster[48104):2020-05-2304:46:14.581EDT [48104LOG:Listening on IPv4 ddress "127.0.0.1",port543 May 23 04:46:14 ocalhost,ocaldomain postaster[48104]:2020-05-23 04:46:14.582 EDT48104] LOG:stening o Unix socket/var/run/postgresq/.s.PGSQL.5432 May2304:46:14 Localhost.Localdomain postmaster[48104]:2020-05-2304:46:14.583EDT[48104]LOG: listening on Unix socket"/tmp/.s.PGSQL.5432" May23 04:46:14 Localhost.Localdomain postmaster[48104]:2020-05-2304:46:14.590EDT [48104] LOG: redirecting log output to Logging collector process May 23 04:46:14 localhost, localdomain postmaster[48104]:2020-05-23 04:46:14.590EDT[48104] HINT:Future og output will appear in directory "log May 23 04:46:14 localhost. Localdomain systemd[1]:Started PostgreSQL database server. [root@Localhost pam.d]#ls/usr/share/doc/postgresq1/README.rpm-dist /usr/share/doc/postgresgL/README.rpm-dist)

106执行如下命令,修改数据库密码。

passwd postgres

:::note 因为安装完成后,系统会创建一个数据库超级用户postgres,密码为空。需要执行passwd postgres命令,修改数据库用户postgres的密码。 :::

功能测试

107执行su - postgres命令,切换到postgres用户。

108执行psql命令,连接到PostgresSQL数据库。

[postgres@localhost ~]$ psql

psql (10.5)

Type "help" for help.

postgres=# help

输入"help"来获取帮助信息. postgres=# help 您正在使用psql,这是一种用于访问PostgreSQL的命令行界面 键入:\copyright显示发行条款 \h显示SQL命令的说明 ?显示pgsql命令的说明 \g或者以分号(;)结尾以执行查询 \q退出 postares=#)

109执行SELECT version();命令,查看PostgresSQL版本信息。

110执行如下命令,创建测试数据库pstgdatabase。

postgres=# create database pstgdatabase;

111执行如下命令,切换到pstgdatabase数据库。

postgres=# \c pstgdatabase

:::note 选择数据库,可以执行\l检查可用的数据库列表,利用\c database选择数据库。 :::

112执行如下命令,创建表testtable。

pstgdatabase=# create table testtable(sid integer,sname text,sex char,score integer);

CREATE TABLE

pstgdatabase=#

113执行如下命令,向testtable表中插入4条记录并查询详细信息。

pstgdatabase=# insert into testtable(sid,sname,sex,score) values(001,'zhang','f',75);

INSERT 0 1

pstgdatabase=# insert into testtable(sid,sname,sex,score) values(002,'li','',75);

INSERT 0 1

pstgdatabase=# select * from testtable;

sid | sname | sex | score

-----+-------+-----+-------

1 | zhang | f | 75

2 | li | | 75

(2 行记录)

114执行如下命令,删除id为001的记录后再查询其详细信息。

pstgdatabase=# delete from testtable where sid='001';

DELETE 1

pstgdatabase=# select * from testtable;

sid | sname | sex | score

-----+-------+-----+-------

2 | li | | 75

(1 行记录)

pstgdatabase=# delete from testtable where sid='001'; DELETE 1 pstgdatabase=# select * from testtable; sid| sname sex| score 2|li 75 (1 row) pstgdatabase=#)

115执行\q命令,退出postgresql。

pstgdatabase=#\q