当前位置: 移动技术网 > 科技>操作系统>Linux > Linux系统Centos7下搭建PostgreSQL关系型数据库

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

2020年04月12日  | 移动技术网科技  | 我要评论

上海市安徽商会,重庆中考成绩查询时间,问问搜搜


一、postgresql简介

1、数据库简介

postgresql是一个功能强大的开源数据库系统,具有可靠性、稳定性、数据一致性等特点,且可以运行在所有主流操作系统上,包括linux、unix、windows等。postgresql是完全的事务安全性数据库,完整地支持外键、联合、视图、触发器和存储过程,支持了大多数的sql:2008标准的数据类型,包括整型、数值型、布尔型、字节型、字符型、日期型、时间间隔型和时间型,它也支持存储二进制的大对像,包括图片、声音和视频。对很多高级开发语言有原生的编程接口api,如c/c++、java、等,也包含各种文档。

2、高度开源

postgresql的源代码可以自由获取,它的授权是在非常自由的开源授权下,这种授权允许用户在各种开源或是闭源项目中使用、修改和发布postgresql的源代码。用户对源代码的可以按用户意愿进行任何修改、改进。因此,postgresql不仅是一个强大的企业级数据库系统,也是一个用户可以开发私用、网络和商业软件产品的数据库开发平台。

二、centos7下安装

1、安装rpm

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

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

2、安装客户端

yum install postgresql11

3、安装服务器端

yum install postgresql11-server

4、安装依赖包

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

5、初始化和启动

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

6、重置密码

passwd postgres

7、登录服务

su - postgres
psql

8、安装vim命令

yum -y install vim*

9、配置远程访问

# 修改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 ## 修改后需要重启

10、开放端口

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

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

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

11、重新启动

systemctl restart postgresql-11

三、创建数据库

1、创建用户

create user root01 with password '123456';
create role;

2、创建数据库

create database db_01 owner root01;
create database;

3、权限授予

grant all privileges on database db_01 to root01;
grant

4、退出命令

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

四、基本操作

1、创建表结构

-- 用户表
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")
);

2、写入数据

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');

3、常规查询

-- 基础查询
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;

4、更新和删除

-- 更新数据
update pq_user set "create_time"='2020-04-09 19:49:57' where ("id"='2');
-- 删除记录
delete from pq_user where "id" = 2;

五、源代码地址

github·地址
https://github.com/cicadasmile/linux-system-base
gitee·地址
https://gitee.com/cicadasmile/linux-system-base
文章转自:https://www.cnblogs.com/guyuyun/p/12683450.html

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网