Linux下PostgreSQL安装部署
目录
1、安装环境
2、pg数据库安装包下载
3、安装依赖包
4、安装
5、测试
一、安装环境
- postgresql-16.0
- CentOS-7
二、pg数据库安装包下载
- 下载地址:下载链接 http://www.postgresql.org/ftp/source/
选择自己要安装的版本
三、安装需要的依赖包
- 在安装数据库的Linux上安装以下依赖包:
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
四、安装
- 创建pgsql文件夹,将安装包放到该目录下
[root@localhost opt]# mkdir pgsql
- 解压压缩包
[root@localhost pgsql]# tar -zxvf postgresql-11.1.tar.gz
- 进入解压后的文件夹
[root@localhost postgresql-16.0]# ls
aclocal.m4 config.log configure contrib doc GNUmakefile.in INSTALL meson.build README
config config.status configure.ac COPYRIGHT GNUmakefile HISTORY Makefile meson_options.txt src
- 编译postgresql源码
--prefix 指定pg安装路径
[root@localhost postgresql-16.0]# ./configure --prefix=opt/pgsql/postgresql
[root@localhost postgresql-16.0]# make
[root@localhost postgresql-16.0]# make install
执行完以上命令,进入opt/pgsql/postgresql路径,查看文件已存在,安装完成。
[root@localhost postgresql]# ls
bin include lib share
| 选项 | 描述 |
|---|---|
| –prefix=prefix | 安装到prefix指向的目录;默认为/usr/local/pgsql |
| –bindir=dir | 安装应用程序到dir;默认为prefix/bin |
| –with-docdir=dir | 安装文档到dir;默认为prefix/doc |
| –with-pgport=port | 设置默认的服务器端网络连接服务TCP端口号 |
| –with-tcl | 为服务端提供Tcl存储过程支持 |
| –with-perl | 为服务端提供Perl存储过程支持 |
| –with-python | 为服务端提供Python存储过程支持 |
- 创建用户组postgres并创建用户postgres
[root@localhost postgresql]# groupadd postgres
[root@localhost postgresql]# useradd -g postgres postgres
[root@localhost postgresql]# id postgres
uid=1000(postgres) gid=1000(postgres) 组=1000(postgres)
- 创建pg数据库的数据主目录且修改文件所有者
[root@localhost postgresql]# mkdir data
[root@localhost postgresql]# chown postgres:postgres data
[root@localhost postgresql]# ls -al
总用量 20
drwxr-xr-x. 7 root root 68 5月 7 18:27 .
drwxr-xr-x. 4 root root 77 5月 7 17:52 ..
drwxr-xr-x. 2 root root 4096 5月 7 17:52 bin
drwx------. 19 postgres postgres 4096 5月 9 17:37 data
drwxr-xr-x. 6 root root 4096 5月 7 17:52 include
drwxr-xr-x. 4 root root 4096 5月 7 17:52 lib
drwxr-xr-x. 6 root root 4096 5月 7 17:52 share
- 配置环境变量
进入home/postgres目录可以看到.bash_profile文件。
编辑修改.bash_profile文件,添加以下内容。
[root@localhost home]# cd postgres
[root@localhost postgres]# ls -al
总用量 24
drwx------. 3 yang postgres 143 5月 8 23:02 .
drwxr-xr-x. 3 root root 18 5月 9 18:19 ..
-rw-------. 1 postgres postgres 613 5月 8 23:03 .bash_history
-rw-r--r--. 1 postgres postgres 18 11月 25 2021 .bash_logout
-rw-r--r--. 1 postgres postgres 307 5月 7 18:48 .bash_profile
-rw-r--r--. 1 postgres postgres 276 5月 8 23:02 .bashrc
drwxr-xr-x. 4 postgres postgres 39 2月 23 01:41 .mozilla
-rw-------. 1 postgres postgres 541 5月 9 00:01 .psql_history
-rw-------. 1 postgres postgres 7 5月 7 18:51 .python_history
[root@localhost postgres ]# vi .bash_profile
export PGHOME=/opt/pgsql/postgresql
export PGDATA=/opt/pgsql/postgresql/data
PATH=$PATH:$HOME/bin:$PGHOME/bin
保存,退出vi。执行以下命令,使环境变量生效。
[root@localhost postgres ]# source .bash_profile
- 切换用户到postgres并使用initdb初使化数据库
[root@localhost postgres ]# su - postgres
[postgres@localhost ~]$ initdb
进入/opt/pgsql/postgresql/data目录
[postgres@localhost data]$ ls
base pg_hba.conf pg_notify pg_stat pg_twophase postgresql.auto.conf serverlog
global pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
pg_commit_ts pg_logical pg_serial pg_subtrans pg_wal postmaster.opts
pg_dynshmem pg_multixact pg_snapshots pg_tblspc pg_xact postmaster.pid
-
配置
修改/pgsql/postgresql/data目录下的两个文件
1、postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
2、pg_hba.conf 配置对数据库的访问权限。
[postgres@localhost data]$ vi postgresql.conf
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)
其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。
[postgres@localhost data]$ vi pg_hba.conf
添加以下内容,局域网才可以访问。
### IPv4 local connections:
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust
- 设置PostgreSQL开机自启动
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
进入一下路径:/opt/pgsql/postgresql-16.0/contrib/start-scripts
[postgres@localhost start-scripts]$ ls
freebsd linux macos
切换为root用户,添加linux文件X属性
[root@localhost start-scripts]# chmod a+x linux
复制linux文件到/etc/init.d目录下,更名为postgresql
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql
修改/etc/init.d/postgresql文件的两个变量
prefix设置为postgresql的安装路径:/opt/pgsql/postgresql
PGDATA设置为postgresql的数据目录路径:opt/pgsql/postgresql/data
设置postgresql服务开机自启动
[root@localhost init.d]$ chkconfig --add postgresql
查看开机自启动服务设置成功。
[root@localhost init.d]$ chkconfig
postgresql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
开放5432端口
firewall-cmd --add-port=5432/tcp --permanent
查看端口是否已开
firewall-cmd --query-port=8888/tcp
执行service postgresql start,启动PostgreSQL服务
[root@localhost init.d]$ service postgresql start
查看PostgreSQL服务
[root@localhost init.d]$ ps -ef | grep postgres
五、测试
切换为postgres用户,进入客户端:
$ su - postgres
$ psql



9251

被折叠的 条评论
为什么被折叠?



