CentOS7安装PostgreSQL数据库

2023-07-21 16:57:28 4012

CentOS7 yum安装PostgreSQL

PostgresSQL的安装
PostgresSQL官网:https://www.landui.com/

1、安装rpm文件

yum install -y https://www.landui.com/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2、安装客户端

yum install postgresql15

3、安装服务端

yum install -y postgresql15-server

4、初始化

/usr/pgsql-15/bin/postgresql-15-setup initdb

5、设置自动启动并且启动postgresql服务

systemctl enable postgresql-15

systemctl start postgresql-15

二、创建用户和数据库

1、使用postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)

su - postgres

image.png

2、登录postgresql数据库

image.png

3、创建用户和数据库并授权

create user test_user with password 'abc_123';              // 创建用户

create database test_db owner test_user;                  // 创建数据库

grant all privileges on database test_db to test_user;        // 授权

image.png

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

\\q

image.png

三、开启远程访问

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

vi /var/lib/pgsql/15/data/postgresql.conf

image.png

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

vi /var/lib/pgsql/15/data/pg_hba.conf

image.png

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

systemctl restart postgresql-15.service

4、使用数据库连接工具测试连接

Linux远程链接命令:

# 格式

psql -h 主机IP -p 端口  -U 用户名 -W -d 数据库

# 示例

psql -h 127.0.0.1 -p 5432  -U test_user -d test_db;

image.png

Windows中使用工具连接测试:

image.png

image.png


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: