Linux环境PostgreSQL源码编译安装

13 篇文章 0 订阅

一、pg数据库安装包下载

下载地址:http://www.postgresql.org/ftp/source/
在这里插入图片描述
下载tar.gz版
在这里插入图片描述

二、安装依赖包

由于是centOS7,所以使用yum来安装

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

三、安装postgres

进入/opt目录,新建两个包,其中software放压缩包,module放解压和安装后的软件

[root@localhost opt]# mkdir software
[root@localhost opt]# mkdir module

然后加入到software中,将下载下来的压缩包传上去

[root@localhost opt]# cd software

然后解压缩

[root@localhost software]# tar -zxvf postgresql-14.4.tar.gz 

然后将解压缩得到的postgresql-14.4移动到module中

[root@localhost software]# mv postgresql-14.4 /opt/module/

进入postgresql-14.4

[root@localhost software]# cd  /opt/module/postgresql-14.4
[root@localhost postgresql-14.4]# ls
aclocal.m4  config.status  contrib    GNUmakefile     INSTALL   src
config      configure      COPYRIGHT  GNUmakefile.in  Makefile
config.log  configure.ac   doc        HISTORY         README

编译postgresql源码,报错

[root@localhost postgresql-14.4]# ./configure --prefix=/opt/module/pgsql
-bash: ./configure: Permission denied

解决此报错的方法是改为输入下面语句:

[root@localhost postgresql-14.4]# bash ./configure --prefix=/opt/module/pgsql

在这里插入图片描述
再先后执行make和make install

[root@localhost postgresql-14.4]# make
[root@localhost postgresql-14.4]# make install

四、创建用户组postgres并创建用户postgres

[root@localhost postgresql-14.4]# groupadd postgres
[root@localhost postgresql-14.4]# useradd -g postgres postgres
[root@localhost postgresql-14.4]# id postgres

五、创建postgresql数据库的数据主目录并修改文件所有者

[root@localhost postgresql-14.4]# cd /opt/module/pgsql/
[root@localhost pgsql]# ls
bin  include  lib  share
[root@localhost pgsql]# mkdir data
[root@localhost pgsql]# chown postgres:postgres data
[root@localhost pgsql]# ls
bin  data  include  lib  share

六、配置环境变量

[root@localhost pgsql]# cd /home/postgres/
[root@localhost postgres]# ls -al
total 28
drwx------. 5 postgres postgres  165 Aug  4 21:07 .
drwxr-xr-x. 4 root     root       35 Aug  4 19:41 ..
-rw-------. 1 postgres postgres 1014 Aug  4 22:20 .bash_history
-rw-r--r--. 1 postgres postgres   18 Apr 10  2018 .bash_logout
-rw-r--r--. 1 postgres postgres  300 Aug  4 19:42 .bash_profile
-rw-r--r--. 1 postgres postgres  231 Apr 10  2018 .bashrc
drwxrwxr-x. 3 postgres postgres   18 Aug  4 19:42 .cache
drwxrwxr-x. 3 postgres postgres   18 Aug  4 19:42 .config
drwxr-xr-x. 4 postgres postgres   39 Aug  4 19:19 .mozilla
-rw-------. 1 postgres postgres   42 Aug  4 21:03 .psql_history
-rw-------. 1 postgres postgres 5275 Aug  4 21:07 .viminfo
[root@localhost postgres]# vim .bash_profile 

在.bash_profile中插入如下内容

export PGHOME=/opt/module/pgsql

export PGDATA=/opt/module/pgsql/data

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

再执行下列语句使其生效

source .bash_profile 

七、切换用户到postgres并使用initdb初使用化数据库

[root@localhost postgres]# su - postgres
Last login: Thu Aug  4 22:16:27 PDT 2022 on pts/0
[postgres@localhost ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /opt/module/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Los_Angeles
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /opt/module/pgsql/data -l logfile start

可以看到/opt/module/pgsql/data中已经有数据了

[postgres@localhost data]$ ls
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION   test.txt
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact

八、配置服务

修改/opt/module/pgsql/data目录下的两个文件。
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。

[postgres@localhost data]$ vim postgresql.conf 

将listen_addresses和port前的注释去掉,并修改listen_addresses的值为*

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

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)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories

进入pg_hba.conf

[postgres@localhost data]$ vim pg_hba.conf 

在IPv4中添加一条语句

# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust

九、设置PostgreSQL开机自启动

找到postgresql-14.4

[postgres@localhost postgresql-14.4]$ pwd
/opt/module/postgresql-14.4
[postgres@localhost postgresql-14.4]$ cd contrib/start-scripts/
[postgres@localhost start-scripts]$ ls
freebsd  linux  macos

切换为root用户

[root@localhost start-scripts]# chmod a+x linux
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql

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

# Installation prefix
prefix=/opt/module/pgsql

# Data directory
PGDATA="/opt/module/pgsql/data"

设置postgresql服务开机自启动

[root@localhost init.d]# chkconfig --add postgresql
[root@localhost init.d]# chkconfig
...
postgresql     	0:off	1:off	2:on	3:on	4:on	5:on	6:off

然后将5432端口开启(或直接把防火墙给关了https://blog.csdn.net/twi_twi/article/details/126176793?spm=1001.2014.3001.5501
执行service postgresql start,启动PostgreSQL服务

[root@localhost init.d]# service postgresql start
Starting PostgreSQL: ok

十、开始测试

默认的用户是postgres,密码和linux系统中所设的postgres用户的密码一样

[root@localhost init.d]# su - postgres
Last login: Thu Aug  4 23:08:40 PDT 2022 on pts/0
[postgres@localhost ~]$ psql
psql (14.4)
Type "help" for help.

postgres=# 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值