前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS 7安装配置PostgreSQL 9.6

CentOS 7安装配置PostgreSQL 9.6

作者头像
星哥玩云
发布2022-08-18 15:40:31
9290
发布2022-08-18 15:40:31
举报
文章被收录于专栏:开源部署开源部署

本文涉及CentOS 7下postgresql的yum安装,访问配置及简单使用。

一.验证环境

1. 操作系统

CentOS-7-x86_64-Everything-1511

2. PostgresSQL版本

PostgreSQL 9.6.3:https://www.postgresql.org/download/linux/RedHat/

二.安装

1. 安装rpm

[root@psql_master ~]# yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

2. 安装客户端

[root@psql_master ~]# yum install -y postgresql96

3. 安装服务器端

#yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL; #同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成,需要进入数据库后修改; #PostgreSQL数据库用户同名的系统账号下登录免密。 [root@psql_master ~]# yum install -y postgresql96-server

4. 初始化

[root@psql_master bin]# /usr/pgsql-9.6/bin/postgresql96-setup initdb

5. 设置开机启动

[root@psql_master ~]# systemctl enable postgresql-9.6

6. 启动

[root@psql_master ~]# systemctl start postgresql-9.6

三.配置使用

1. 修改用户密码

复制代码 #yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL; [root@psql_master ~]# su - postgres

#切换用户后,提示符变更为“-bash-4.2$”; #同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成; #PostgreSQL在数据库用户同名的系统账号下登录免密; -bash-4.2$ psql -U postgres

#进入数据库后修改密码; postgres=# alter user postgres with password 'postgres@123'

2. 允许远程访问

#配置文件中,默认只能本机访问postgresql; #修改listen_addresses = 'localhost'为listen_addresses = '*',允许所有远程访问; #修改配置文件需要重启服务。 [root@psql_master ~]# sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /var/lib/pgsql/9.6/data/postgresql.conf

3. 主机认证

#在第82行之后,”IPv4 local connections”下新增允许的客户端; #“host” 代表主机类型,第一个“all”代表db ,第二个“all”代表user ,“172.29.3.67/32” 代表client ip,“trust”代表认证方式; #认证方式除“trust”外,还有“peer”, “ident”, “md5”, “password”等,具体可参考pg-hba文件: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html #修改pg.hba文件需要重启服务。 [root@psql_master ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf host    all            all            172.29.3.67/32          trust

4. 设置环境变量

[root@psql_master ~]# vim /etc/profile export PATH=$PATH:/usr/pgsql-9.6/bin

[root@psql_master ~]# source /etc/profile

5. 重启服务

[root@psql_master ~]# systemctl restart postgresql-9.6

6. iptables

#postgresql默认开启tcp5432端口 [root@psql_master ~]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

[root@psql_master ~]# service iptables restart

四.使用验证

1. 查看端口

[root@psql_master ~]# netstat -tunlp

2. 简单使用

1)创建用户

postgres=# create user postuser1 with password 'user1@123';

2)创建数据库

#同时指定数据库的所有者 postgres=# create database postdb1 owner postuser1;

3)数据库赋权

#未赋权则账户只能登录控制台 postgres=# grant all privileges on database postdb1 to postuser1;

4)登录新建数据库

#在操作系统层使用新建的账号登录新建的数据库,登录后提示符为“postdb1=>”; #如果在postgres账户下直接使用“postgres=# \c postdb1;”登录,则登录用户依然是postgres, -bash-4.2$ psql -U postuser1 -d postdb1 -h 127.0.0.1 -p 5432

5)创建表

postdb1=> create table tb1(           id int primary key,           name VARCHAR(20),           salary real           );

6)插入数据

postdb1=> insert into tb1(           id, name, salary)           values(           101, 'Mike', 5000.00           );

7)查询

postdb1=>select * from tb1;

3. pgadmin连接postgresql

pgadmin下载地址:https://www.pgadmin.org/download/

截至2017-05-19的版本是:pgAdmin 4 v1.5

1)添加服务器

打开pgadmin—>添加新的服务器—>(通常标签)名称自定义—>(connection标签)主机名称与postgresql用户密码按需填写,其余可采用默认配置—>保存。

2)图形化查看

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档