下载地址: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
进入/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
[root@localhost postgresql-14.4]# make
[root@localhost postgresql-14.4]# make install
[root@localhost postgresql-14.4]# groupadd postgres
[root@localhost postgresql-14.4]# useradd -g postgres postgres
[root@localhost postgresql-14.4]# id postgres
[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
[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-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=#