Linux系统:Centos7下搭建PostgreSQL关系型数据库

本文源码:GitHub·点这里 || GitEE·点这里linux

1、PostgreSQL简介

一、数据库简介git

PostgreSQL是一个功能强大的开源数据库系统,具备可靠性、稳定性、数据一致性等特色,且能够运行在全部主流操做系统上,包括Linux、Unix、Windows等。PostgreSQL是彻底的事务安全性数据库,完整地支持外键、联合、视图、触发器和存储过程,支持了大多数的SQL:2008标准的数据类型,包括整型、数值型、布尔型、字节型、字符型、日期型、时间间隔型和时间型,它也支持存储二进制的大对像,包括图片、声音和视频。对不少高级开发语言有原生的编程接口API,如C/C++、Java、等,也包含各类文档。github

二、高度开源sql

PostgreSQL的源代码能够自由获取,它的受权是在很是自由的开源受权下,这种受权容许用户在各类开源或是闭源项目中使用、修改和发布PostgreSQL的源代码。用户对源代码的能够按用户意愿进行任何修改、改进。所以,PostgreSQL不只是一个强大的企业级数据库系统,也是一个用户能够开发私用、网络和商业软件产品的数据库开发平台。数据库

2、Centos7下安装

一、安装RPM编程

RPM软件包管理器,一种用于互联网下载包的打包及安装工具,它包含在部分Linux分发版中。vim

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

二、安装客户端安全

yum install postgresql11

三、安装服务器端服务器

yum install postgresql11-server

四、安装依赖包网络

yum install postgresql11-libs
yum install postgresql11-contrib
yum install postgresql11-devel

五、初始化和启动

/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11

六、重置密码

passwd postgres

七、登陆服务

su - postgres
psql

八、安装Vim命令

yum -y install vim*

九、配置远程访问

# 修改01
vim /var/lib/pgsql/11/data/postgresql.conf
listen_addresses = 'localhost' 
修改成
listen_addresses = '*'  

# 修改02
vim /var/lib/pgsql/11/data/pg_hba.conf
添加内容
host  all  all  0.0.0.0/0 trust ## 修改后须要重启

十、开放端口

firewall-cmd --query-port=5432/tcp

firewall-cmd --add-port=5432/tcp

firewall-cmd --add-port=5432/tcp --zone=public --permanent

十一、从新启动

systemctl restart postgresql-11

3、建立数据库

一、建立用户

CREATE USER root01 WITH PASSWORD '123456';
CREATE ROLE;

二、建立数据库

CREATE DATABASE db_01 OWNER root01;
CREATE DATABASE;

三、权限授予

GRANT ALL PRIVILEGES ON DATABASE db_01 TO root01;
GRANT

四、退出命令

\q:退出SQL编辑
exit:退出脚本

4、基本操做

一、建立表结构

-- 用户表
CREATE TABLE pq_user (
    ID INT NOT NULL,
    user_name VARCHAR (32) NOT NULL,
    user_age int4 NOT NULL,
    create_time TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT "pg_user_pkey" PRIMARY KEY ("id")
);

-- 订单表
CREATE TABLE pq_order (
    id int not null,
    user_id int not null,
    order_no varchar (32) not null,
    goods varchar (20) not null,
    price money not null,
    count_num int default 1, 
    create_time timestamp (6) default current_timestamp,
    constraint "pq_order_pkey" primary key ("id")
);

二、写入数据

INSERT INTO pq_user ("id", "user_name", "user_age", "create_time") 
VALUES ('1', 'user01', '18', '2020-04-09 19:44:57.16154');
INSERT INTO pq_order ("id", "user_id", "order_no", "goods", "price", "count_num", "create_time") 
VALUES ('1', '1', 'NO20200329652362', '书籍', '$12.20', '3', '2020-04-09 20:01:09.660208');

三、常规查询

-- 基础查询
select * from pq_user t1 where t1.id='2' and t1.user_name='user01';
select * from pq_user t1 where t1.id !='2' order by create_time desc;
-- 链接查询
select * from pq_user t1 join pq_order t2 on t1.id=t2.user_id;
select * from pq_user t1 left join pq_order t2 on t1.id=t2.user_id;

四、更新和删除

-- 更新数据
UPDATE pq_user SET "create_time"='2020-04-09 19:49:57' WHERE ("id"='2');
-- 删除记录
DELETE FROM pq_user WHERE "id" = 2;

5、源代码地址

GitHub·地址
https://github.com/cicadasmile/linux-system-base
GitEE·地址
https://gitee.com/cicadasmile/linux-system-base

Linux系统:Centos7下搭建PostgreSQL关系型数据库

推荐阅读:环境安装

序号 文章标题
01 Centos7下安装Jdk八、Tomcat八、MySQL5.7环境
02 Centos7下搭建Redis单台和Redis集群服务
03 Centos7下搭建Rocketmq4.3中间件,配置监控台
04 Centos7下搭建ZooKeeper3.4中间件,经常使用命令总结
05 Centos7下搭建ElasticSearch中间件,经常使用接口演示
06 Centos7下搭建Nginx,FastDFS文件管理中间件
07 Centos7下搭建ClickHouse列式存储数据库
相关文章
相关标签/搜索