安装posgresql(端口5432)
1、先将下载好的依赖包执行:rpm -Uvh *.rpm --nodeps --force加载
--nodeps --force为跳过依赖
再执行安装:yum -y install gcc-c++
yum install -y readline-devel
yum install zlib-devel;
2、解压安装包
tar -zvxf postgresql-12.9.tar.gz
3、配置
cd postgresql-12.9
./configure --prefix=/opt/pgsql
编译:make world (如果报错是依赖包少了)
安装:make install-world
4、增加postgres用户
adduser postgres
如果提示:adduser: cannot open /etc/passwd,以次使用chattr -i /etc/passwd命令进行增加读写权限
设置postgres用户密码:passwd postgres
5、创建数据库目录,并赋予postgres用户权限
mkdir /opt/pgsql/data
chown -R postgres:postgres /opt/pgsql/data
切换到postgres用户:su - postgres
6、初始化数据库
/opt/pgsql/bin/initdb -D /opt/pgsql/data
启动数据库:/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start
创建数据库:/opt/pgsql/bin/createdb sthx
进入数据库:/opt/pgsql/bin/psql sthx
修改默认postgres数据库用户的密码:xwgh=# ALTER USER postgres WITH PASSWORD 'Bxwl@123';
7.配置远程访问posgresql权限
su - postgres
(1)修改pg_hba.conf文件,在ipv4 的连接配置处,增加配置# 所有的用户通过任意IP都可以使用面膜的方式登录PostgreSQL
vi /opt/pgsql/data/pg_hba.conf在ipv4下配置
host all all 0.0.0.0/0 password
(2).修改postgresql.conf文件,在Connection Settings的配置处,增加配置(# - Connection Settings -)
vi /opt/pgsql/data/postgresql.conf
listen_addresses = '*'
(3).重启postgresql
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile restart
8、创建自启
(1)创建一个新的systemd服务文件
进入system目录:cd /etc/systemd/system/目录下,
编辑service文件:vim postgresql.service并填入以下内容:
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# 这里的路径需要根据你的实际安装路径来设置
ExecStart=/opt/pgsql/bin/pg_ctl start -D /optl/pgsql/data
ExecStop=/opt/pgsql/bin/pg_ctl stop -D /opt/pgsql/data
ExecReload=/opt/pgsql/bin/pg_ctl reload -D /opt/pgsql/data
[Install]
WantedBy=multi-user.target
(2)重新加载systemd管理器配置: sudo systemctl daemon-reload
(3)启用PostgreSQL服务以在启动时运行: sudo systemctl enable postgresql
(4)你可以选择立即启动PostgreSQL服务: sudo systemctl start postgresql