详细内容
centOS7安装postgreSQL
发布日期:2025-04-30     点击:11     字体:[ ]

1.使用官方的的yum仓库安装,据说在PostgreSQL的整个支持生命周期内为所有支持的PostgreSQL版本提供自动更新。参考页面:https://www.postgresql.org/download/linux/redhat/,这里以安装13.20版本为例。

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql13-server
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13


2.如果需要用到扩展,需要安装扩展,比如要用dblink之类的

yum install postgresql13-contrib


3.改配置文件(默认路径:/var/lib/pgsql/13/data/postgresql.conf),默认只支持本地连接

重点关注这句,

listen_addresses='*'

改为*,表示支持所有连接。


另外还要注意防火墙,要开放5432端口,即postgresql的默认端口,执行一下命令添加5432开放端口

firewall-cmd --zone=public --add-port=5432/tcp --permanent

4.改pg_hba.conf,用于登录控制

# IPv4 local connections:

host    all             all             127.0.0.1/32            trust

host    all             all            0.0.0.0/0            md5


如果需要为某个库添加扩展,可在服务器登录psql,其中testdb是自己建的库名

psql -h localhost -U postgres -d testdb

例如安装dblink扩展

create extension dblink;

查看已安装的扩展

\dx

退出

\q

默认postgres没密码,远程连接需要密码,可登录psql修改密码

ALTER USER postgres WITH PASSWORD 'postgres';


停止、启动可使用systemctl,例如

systemctl stop postgresql-13

查看状态

systemctl status postgresql-13

启动

systemctl start postgresql-13

重启

systemctl restart postgresql-13


5.日志

默认路径/var/lib/pgsql/13/data/log,可在配置文件里修改


6.其它备注

除了可使用systemctl启动关闭,也可以使用pg_ctl

例如开启数据库服务器:

/usr/pgsql-13/bin/pg_ctl -D /opt/pgsql13/data -l 日志文件 start

可以把pg_ctl加入环境变量,就可以直接使用pg_ctl启动停止服务

可以用postgres用户编辑.bash_profile文件


[ -f /etc/profile ] && source /etc/profile

PGHOME=/usr/pgsql-13

PGDATA=/opt/pgsql13/data

PATH=$PGHOME/bin:$PATH:$HOME/.local/bin:$HOME/bin

export PATH

export PGDATA

export PGHOST=/run/postgresql

# If you want to customize your settings,

# Use the file below. This is not overridden

# by the RPMS.

[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile


然后source .bash_profile就可以使用pg_ctl了,例如

pg_ctl -D /opt/pgsql13/data -l /opt/pgsql13/data/log/pg.log start


另外root用户可以使用systemctl命令操作服务启停,例如

启动:systemctl start postgresql-13

停止:systemctl stop postgresql-13

重启:systemctl restart postgresql-13

查看状态:systemctl status postgresql-13


有一点特别要注意:

用systemctl启动的服务,pg_ctl查看状态是正常的,但如果用pg_ctl启动服务,用systemctl查看状态却不正常


参考:https://www.cnblogs.com/hxlasky/p/16844609.html


用户评论
昵称 
内容  *
验证码   
   
Copyright © 2010 zdbase.com All Rights Reserved. 苏ICP备15039389号 可人软件设计