«

2022年2月17日 Linux下PostgreSQL安装部署(2022年3月21日完成)

时间:2022-2-17 10:20     作者:Mahalalel     分类: Linux


1、下载安装包

1.1、下载页面

安装包下载页面:https://www.postgresql.org/ftp/source/

PostgreSQL下载页面

这里选择v14.2版本。

安装包下载页面
复制下载地址

下载地址如下:https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz

1.2、服务器上获取安装包

获取安装包命令:

wget https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz

下载安装包

2、安装PostgreSQL

2.1、创建文件夹并赋予权限

命令如下:

mkdir postgresql
chmod -R 777 postgresql

2.2、解压文件

命令:

tar -zxvf postgresql-14.2.tar.gz -C /usr/local/postgresql

2.3、移动文件夹

mv /usr/local/postgresql/postgresql-14.2/* /usr/local/postgresql/

2.4、安装

./configure --prefix=/usr/local/postgresql

在执行 configure 命令过程中报以下错误,configure: error: readline library not found ,可是我在系统中安装readline包了。
参考链接:安装postgreSQL出现configure:error:readline library not found解决方法

rpm -qa | grep readline

显示:

readline-7.0-10.el8.x86_64

通过 yum 搜索相关的 readline 包

yum search readline

结果中显示:

readline-devel.i686 : Files needed to develop programs which use the readline library
readline-devel.x86_64 : Files needed to develop programs which use the readline library

那么安装readline-devel包

yum -y install -y readline-devel

再次执行“configure”命令;
此时没有任何error;
接下来,就是编译以及安装

make && make install

2.3、创建系统用户

useradd postgres
chown -R postgres:postgres /usr/local/postgresql

2.4、初始化数据库

# 切换用户
su - postgres
# 初始化数据库
/usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data

初始化数据库

2.5、修改配置

# 修改监听地址
vim data/postgresql.conf
listen_addresses = '*'
# 修改安全策略
vim data/pg_hba.conf
host all all 0.0.0.0/0 trust

3、启动数据库

启动postgresql
pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/log/pg.log restart

3-1、设置开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
linux文件即为linux系统上的启动脚本

[postgres@VM-16-8-centos contrib]$ cd start-scripts/
[postgres@VM-16-8-centos start-scripts]$ ls
freebsd  linux  macos

a)切换超管用户,修改linux文件属性,添加X属性

[root@VM-16-8-centos ~]# cd /usr/local/postgresql/contrib/start-scripts/
[root@VM-16-8-centos start-scripts]# chmod a+x linux 

b)复制linux文件到/etc/init.d目录下,更名为postgresql

[root@VM-16-8-centos start-scripts]# cp linux /etc/init.d/postgresql

c)修改/etc/init.d/postgresql文件中的两个变量

prefix设置为postgresql的安装路径:/usr/local/postgresql

PGDATA设置为postgresql的数据目录路径:/usr/local/postgresql/data

d)设置postgresql服务开机自启动

[root@VM-16-8-centos start-scripts]# chkconfig --add postgresql

查看开机自启动服务设置

[root@VM-16-8-centos start-scripts]# chkconfig

postgresql      0:off   1:off   2:on    3:on    4:on    5:on    6:off
redis           0:off   1:off   2:on    3:on    4:on    5:on    6:off

3-2、登录网页控制台开放5432端口

a)启动PostgreSQL服务

[root@VM-16-8-centos local]# service postgresql start
Starting PostgreSQL: ok

b)查看PostgreSQL服务

[root@VM-16-8-centos local]# ps -ef|grep postgres
root     2824636 2812646  0 12:21 pts/0    00:00:00 su - postgres
postgres 2824637 2824636  0 12:21 pts/0    00:00:00 -bash
postgres 2865229       1  0 13:49 ?        00:00:00 /usr/local/postgresql/bin/postmaster -D /usr/local/postgresql/data
postgres 2865231 2865229  0 13:49 ?        00:00:00 postgres: checkpointer 
postgres 2865232 2865229  0 13:49 ?        00:00:00 postgres: background writer 
postgres 2865233 2865229  0 13:49 ?        00:00:00 postgres: walwriter 
postgres 2865234 2865229  0 13:49 ?        00:00:00 postgres: autovacuum launcher 
postgres 2865235 2865229  0 13:49 ?        00:00:00 postgres: stats collector 
postgres 2865236 2865229  0 13:49 ?        00:00:00 postgres: logical replication launcher 
root     2865336 2826175  0 13:49 pts/0    00:00:00 grep --color=auto postgres

4、测试

切换postgres用户,进入客户端

[root@VM-16-8-centos local]# su - postgres
[postgres@VM-16-8-centos ~]$ psql

创建数据库用户 --->>> 赋予账号权限 --->>> 新建数据库 --->>> 退出

psql (14.2)
Type "help" for help.

postgres=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(3 rows)

postgres=# create user mahalalel password '*******';
CREATE ROLE
postgres=# alter ROLE mahalalel SUPERUSER;
ALTER ROLE
postgres=# create database postgre;
CREATE DATABASE
postgres=# \q

重新登录数据库 --->>> 输入密码 --->>> 显示数据库

[postgres@VM-16-8-centos ~]$ psql -U mahalalel -d postgre
psql (14.2)
Type "help" for help.

postgre=# \l
                                 List of databases
   Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges   
-----------+----------+----------+------------+------------+-----------------------
 postgre   | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
           |          |          |            |            | postgres=CTc/postgres
(4 rows)

postgre=# 

参考链接:Linux下PostgreSQL安装部署详细步骤

标签: PostgreSQL安装 Linux环境