微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL安装教程CentOS

Postgresql安装教程(CentOS)

一、安装教程

1.1 安装脚本

官网安装脚本查询https://www.postgresql.org/download/linux/redhat/

image-20210219145345087

 # Install the repository RPM:
 sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
 ​
 # Install Postgresql:
 sudo yum install -y postgresql10-server
 ​
 # Optionally initialize the database and enable automatic start:
 sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
 sudo systemctl enable postgresql-10
 sudo systemctl start postgresql-10

二、创建用户数据库

2.1 切换用户

使用postgres用户登录(Postgressql安装后会自动创建postgres用户,无密码

 [root@localhost software]# su postgres
 bash-4.2$

2.2 登录postgresql数据库

 bash-4.2$ psql
 psql (10.16)
 输入 "help" 来获取帮助信息.

2.3 创建用户数据库并授权

 create user test_user with password 'abc123';            # 创建用户
 create database test_db owner test_user;                 # 创建数据库
 grant all privileges on database test_db to test_user;   # 授权

image-20210219150310580

2.4 退出psql(输入 \q 再按回车键即可)

 postgres=# \q
 bash-4.2$

三、开启远程访问

3.1 修改监听地址

修改/var/lib/pgsql/10/data/postgresql.conf文件,取消 listen_addresses 的注释,将参数值改为“*”

 bash-4.2$ vim /var/lib/pgsql/10/data/postgresql.conf

image-20210219150913157

3.2 添加本地连接地址

修改/var/lib/pgsql/10/data/pg_hba.conf文件增加下图红框部分内容

 bash-4.2$ vim /var/lib/pgsql/10/data/pg_hba.conf

image-20210219151221148

3.3 切换到root用户,重启postgresql服务

 bash-4.2$ su root
 密码
 [root@localhost software]# systemctl restart postgresql-10.service

3.4 使用数据库连接工具测试连接

image-20210219151517372

四、其它补充

4.1修改postgres 用户密码

修改生成的 postgres 用户密码(此postgres非上面的postgres用户,此为数据库用户,上面的为操作系统的用户

su - postgres
psql -U postgres
alter user postgres with encrypted password '1';

image-20210219151921239

image-20210219152131029

4.2 服务启动、关闭、重启、查看状态命令

systemctl start postgresql-10.service     # 启动服务
systemctl stop postgresql-10.service # 关闭服务
systemctl restart postgresql-10.service # 重启服务
systemctl status postgresql-10.service # 查看状态

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐