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 # 查看状态

 

posted @ 2021-11-04 01:03  灬丨嘎嘣脆丨灬  阅读(91)  评论(0编辑  收藏  举报