阿里云安装的postgresql 允许远程连接
官网安装方法:https://www.postgresql.org/download/linux/redhat/
卸载的话使用 yum remove 相应的安装
Install the repository RPM:
yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-1.noarch.rpm
install the server packages:
yum install postgresql10-server
此时在/usr/下多了pgsql-10目录
关于安装的几个软件的介绍:
postgresql-client libraries and client binaries
postgresql-server core database server
postgresql-contrib additional supplied modules
postgresql-devel libraries and headers for C language development
pgadmin3 - pgAdmin III graphical administration utility
关于server和client的介绍参见:
https://www.postgresql.org/docs/10/static/tutorial-start.html
安装后:
设置环境变量:
略
初始化数据库:
/usr/pgsql-10/bin/postgresql-10-setup initdb
可选设置自动启动:
systemctl enable postgresql-10
systemctl start postgresql-10
启动服务:
service postgresql-10 initdb
chkconfig postgresql-10 on
直接执行createdb后会有很多问题,具体看https://www.postgresql.org/docs/10/static/tutorial-start.html
PostgreSQL 安装完成后,会建立一下‘postgres’用户,用于执行PostgreSQL,数据库中也会建立一个’postgres’用户,默认密码为自动生成,需要在系统中改一下。
修改postgres用户密码:
先切换到root
然后passwd postgress
接着输入两遍新密码
修改postgres数据库管理员密码:
su - postgres 切换用户,执行后提示符会变为 ‘-bash-4.2$’,切换为UNIX风格的bash
psql -U postgres 登录数据库,执行后提示符变为 ‘postgres=#’
ALTER USER postgres WITH PASSWORD ‘abc123’ 设置postgres用户密码
\q 退出数据库
切换到root配置一下远程连接。
开启远程访问
vi /var/lib/pgsql/10/data/postgresql.conf
修改#listen_addresses = ‘localhost’ 为 listen_addresses=’*’
当然,此处‘*’也可以改为任何你想开放的服务器IP
信任远程连接
vi /var/lib/pgsql/10/data/pg_hba.conf
修改如下内容,信任指定服务器连接
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.2/32(需要连接的服务器IP) trust
远程连接配置完成,由于系统原因,还需要在防火墙中打开相应的端口。
最后一步,不能忘记的,是重启数据库服务,使配置生效。
重启PostgreSQL数据服务
systemctl restart postgresql-10.service
下面是postgersSQL的使用,在使用postgres的时候要在postgres用户下使用。
createdb mydb
执行这句的时候回出现很多问题,具体见:doc
下面是常见的一种错误:
createdb: could not connect to database postgres: FATAL: role “joe” does not exist
这个是因为没有以postgres用户执行的原因。
执行SQL语句:
psql mydb
/////////////////////////////////////////////////////
1.添加网络安全组规则
添加端口号为5432的入规则,允许 ip为0.0.0.0/0
2.修改配置文件
1. 修改postgresql.conf文件(postgresql12默认不需要进行此步更改),将数据库服务器的监听模式修改为监听所有主机发出的连接请求。
定位到#listen_addresses=’localhost’。PostgreSQL安装完成后,默认是只接受来在本机localhost的连接请求。
将行开头都#去掉,将行内容修改为listen_addresses=’*'来允许数据库服务器监听来自任何主机的连接请求。
2. 修改pg_hba.conf文件,配置用户的访问权限(#开头的行是注释内容):
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.0/24 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
其中,第7条是新添加的内容,表示允许所有主机使用所有合法的数据库用户名访问数据库,并提供加密的密码验证。
其中,第8条是新添加的内容,表示允许所有主机使用所有合法的数据库用户名访问数据库,并提供加密的密码验证。
其中,数字24是子网掩码,表示允许192.168.1.0--192.168.1.255的计算机访问!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了