Linux 上安装 PostgreSQL 数据库

本文详细介绍了在CentOS7系统中安装PostgreSQL9.6数据库的步骤,包括下载依赖包、安装数据库、配置启动项、设置访问权限、重启数据库、修改默认用户密码以及放开防火墙端口限制。最后,文章提醒读者通过工具测试数据库连接以确认安装成功。
摘要由CSDN通过智能技术生成
一、安装依赖
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libicu-50.2-4.el7_7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-9.6.20-1PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-libs-9.6.20-1PGDG.rhel7.x86_64.rpm
wget https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/postgresql96-server-9.6.20-1PGDG.rhel7.x86_64.rpm
二、安装数据库
rpm -ivh libicu-50.2-4.el7_7.x86_64.rpm --force --nodeps
rpm -ivh postgresql96-libs-9.6.20-1PGDG.rhel7.x86_64.rpm --force --nodeps
rpm -ivh postgresql96-9.6.20-1PGDG.rhel7.x86_64.rpm --force --nodeps
rpm -ivh postgresql96-server-9.6.20-1PGDG.rhel7.x86_64.rpm --force --nodeps
三、配置启动项
#初始化数据库
/usr/pgsql-9.6/bin/postgresql96-setup initdb
#开机启动
systemctl enable postgresql-9.6
#启动pg
systemctl start postgresql-9.6
四、访问权限配置
1、放开所有 IP 连接

执行如下命令修改配置文件:

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

将 listen_addresses 内容改为 * 号,参考如下:

listen_addresses = '*'          # what IP address(es) to listen on;
                                # comma-separated list of addresses;
                                # defaults to 'localhost'; use '*' for all
                                # (change requires restart)
port = 5432                     # (change requires restart)
2、取消 IP 连接限制

执行如下命令修改配置文件:

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

主要添加如下内容:

host    all             all              0.0.0.0/0               trust

参考如下:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident
host    all             all              0.0.0.0/0               trust
五、重启数据库
systemctl restart postgresql-9.6
六、测试数据库是否安装成功
[root@localhost pgsql-9.6]# su - postgres
e-bash-4.2$ psql
psql (9.6.20)
输入 "help" 来获取帮助信息
七、修改默认用户postgres密码
[root@localhost pgsql-9.6]# su - postgres
上一次登录:二 6月 15 14:40:17 CST 2021pts/0 上
e-bash-4.2$ psql
psql (9.6.20)
输入 "help" 来获取帮助信息.

postgres=# ALTER USER postgres WITH PASSWORD '123456';
ALTER ROLE
八、重新读取数据库配置
[root@localhost pgsql-9.6]# su - postgres
上一次登录:二 6月 15 15:04:03 CST 2021pts/0 上
-bash-4.2$ psql -U postgres
psql (9.6.20)
输入 "help" 来获取帮助信息.

postgres=# select pg_reload_conf()
postgres-# ;
 pg_reload_conf 
----------------
 t
(1 行记录)
九、放开防火墙端口限制

执行如下命令即可

[root@localhost pgsql-9.6]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost pgsql-9.6]# iptables -I INPUT -p tcp --dport 5432 -j ACCEPT

以上就是安装过程,安装完成之后使用工具测试连接数据库吧。

参考内容如下,侵删:

https://www.jianshu.com/p/6d79f56d039b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值