Linux环境PostgreSQL源码编译安装
一、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@echohye opt]# mkdir software
[root@echohye opt]# mkdir module
然后加入到software中,将下载下来的压缩包传上去
[root@echohye opt]# cd software
然后解压缩
[root@echohye software]# tar -zxvf postgresql-15.8.tar.gz
然后将解压缩得到的postgresql-15.8移动到module中
[root@echohye software]# mv postgresql-15.8 /opt/module/
进入postgresql-15.8
[root@echohye software]# cd /opt/module/postgresql-15.8
[root@echohye postgresql-15.8]# ls
aclocal.m4 config.status contrib GNUmakefile INSTALL src
config configure COPYRIGHT GNUmakefile.in Makefile
config.log configure.ac doc HISTORY README
编译postgresql源码,报错
[root@echohye postgresql-15.8]# ./configure --prefix=/opt/module/pgsql
-bash: ./configure: Permission denied
解决此报错的方法是改为输入下面语句:
[root@echohye postgresql-15.8]# bash ./configure --prefix=/opt/module/pgsql
再先后执行make和make install
[root@echohye postgresql-15.8]# make // make -j$(nproc)
[root@echohye postgresql-15.8]# make install
四、创建用户组postgres并创建用户postgres
[root@echohye postgresql-15.8]# groupadd postgres
[root@echohye postgresql-15.8]# useradd -g postgres postgres
[root@echohye postgresql-15.8]# id postgres
五、创建postgresql数据库的数据主目录并修改文件所有者
[root@echohye postgresql-15.8]# cd /opt/module/pgsql/
[root@echohye pgsql]# ls
bin include lib share
[root@echohye pgsql]# mkdir data
[root@echohye pgsql]# chown postgres:postgres data
[root@echohye pgsql]# ls
bin data include lib share
六、配置环境变量
[root@echohye pgsql]# cd /home/postgres/
[root@echohye 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@echohye 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@echohye postgres]# su - postgres
Last login: Thu Aug 4 22:16:27 PDT 2022 on pts/0
[postgres@echohye ~]$ 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@echohye 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@echohye 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@echohye 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-15.8
[postgres@echohye postgresql-15.8]$ pwd
/opt/module/postgresql-15.8
[postgres@echohye postgresql-15.8]$ cd contrib/start-scripts/
[postgres@echohye start-scripts]$ ls
freebsd linux macos
切换为root用户
[root@echohye start-scripts]# chmod a+x linux
[root@echohye 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@echohye init.d]# chkconfig --add postgresql
[root@echohye 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@echohye init.d]# service postgresql start
Starting PostgreSQL: ok
十、开始测试
默认的用户是postgres,密码和linux系统中所设的postgres用户的密码一样
[root@echohye init.d]# su - postgres
Last login: Thu Aug 8 23:08:40 PDT 2024 on pts/0
[postgres@echohye ~]$ psql
psql (15.8)
Type "help" for help.
[postgres@echohye ~]$ ALTER USER postgres WITH PASSWORD '123456'; // 修改密码
[postgres@echohye ~]$ \q // 退出
转载自https://blog.csdn.net/twi_twi/article/details/126176036