postgrqSQL14.2-源码安装

1、安装环境

环境:

  • postgresql-14.2
  • CentOS Linux release 7.4.1708 (Core)
  • 注意:确认linux系统可以正常连接网络,因为在后面需要添加依赖包。

2、pg数据库安装包下载

下载地址:http://www.postgresql.org/ftp/source/

清华源地址:https://mirrors.tuna.tsinghua.edu.cn/postgresql/source/

选择要安装的版本进行下载:

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

3、安装依赖包

在要安装postgresql数据库的Linux服务器(hostname:weekend02)上执行以下命令安装所需要的依赖包:

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

4、安装postgres

安装步骤

  1. 在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。
  2. 解压压缩包
  3. 进入解压后的文件夹
  4. 编译postgresql源码
# 1. 在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。
[root@i-vfyxrtmx soft]# ls
postgresql-14.2.tar.gz

# 2、创建目录,解压压缩包到目录中
[root@i-vfyxrtmx soft]# tar -xzvf postgresql-14.2.tar.gz -C pg_setup/
# 或者直接解压也可以
[root@i-vfyxrtmx soft]# tar -xzvf postgresql-14.2.tar.gz

# 3、直接解压后进入解压后的文件夹 
[root@i-vfyxrtmx soft]# cd postgresql-14.2
[root@i-vfyxrtmx postgresql-14.2]# ls
aclocal.m4  config  configure  configure.ac  contrib  COPYRIGHT  doc  GNUmakefile.in  HISTORY  INSTALL  Makefile  README  src

# 4、创建pgsql的目录,编译postgresql源码
[root@i-vfyxrtmx postgresql-14.2]# mkdir -p /opt/pgsql/postgresql
[root@i-vfyxrtmx postgresql-14.2]# ./configure --prefix=/opt/pgsql/postgresql

选项					描述
–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存储过程支持

# 安装
[root@i-vfyxrtmx postgresql-14.2]# make
......
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/opt/soft/postgresql-14.2/src/test/perl'
make[1]: Leaving directory `/opt/soft/postgresql-14.2/src'
make -C config all
make[1]: Entering directory `/opt/soft/postgresql-14.2/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/opt/soft/postgresql-14.2/config'

[root@i-vfyxrtmx postgresql-14.2]# make install
......
make -C config install
make[1]: Entering directory `/opt/soft/postgresql-14.2/config'
/usr/bin/mkdir -p '/opt/pgsql/postgresql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/pgsql/postgresql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/pgsql/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/opt/soft/postgresql-14.2/config

# 至此,已完成postgreql的安装。进入/pgsql/postgresql目录可以看到安装后的postgresql的文件。
[root@i-vfyxrtmx postgresql]# cd /opt/pgsql/postgresql/
[root@i-vfyxrtmx postgresql]# pwd
/opt/pgsql/postgresql
[root@i-vfyxrtmx postgresql]# ls
bin  include  lib  share

5、创建用户 、用户组

# 创建用户组postgres、用户postgres 
[root@weekend02 postgresql-11.1]# groupadd postgres
[root@weekend02 postgresql-11.1]# useradd -g postgres postgres
[root@weekend02 postgresql-11.1]# id postgres
uid=501(postgres) gid=501(postgres)=501(postgres)

6、创建数据主目录

# 创建postgresql数据库的数据主目录并修改文件所有者
# 这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql/postgresql/data目录下:

[root@i-vfyxrtmx postgresql]# cd /opt/pgsql/postgresql
[root@i-vfyxrtmx postgresql]# mkdir data
[root@i-vfyxrtmx postgresql]# chown postgres:postgres data
[root@i-vfyxrtmx postgresql]# ls -al
total 28
drwxr-xr-x 7 root     root     4096 Mar  7 16:25 .
drwxr-xr-x 3 root     root     4096 Mar  7 16:16 ..
drwxr-xr-x 2 root     root     4096 Mar  7 16:23 bin
drwxr-xr-x 2 postgres postgres 4096 Mar  7 16:25 data
drwxr-xr-x 6 root     root     4096 Mar  7 16:23 include
drwxr-xr-x 4 root     root     4096 Mar  7 16:23 lib
drwxr-xr-x 6 root     root     4096 Mar  7 16:23 share

7、配置环境变量

# 进入home/postgres目录可以看到.bash_profile文件。

[root@i-vfyxrtmx postgresql]# cd /home/postgres/
[root@i-vfyxrtmx postgres]# 
[root@i-vfyxrtmx postgres]# ls -al
total 20
drwx------  2 postgres postgres 4096 Mar  7 16:24 .
drwxr-xr-x. 3 root     root     4096 Mar  7 16:24 ..
-rw-r--r--  1 postgres postgres   18 Aug  3  2017 .bash_logout
-rw-r--r--  1 postgres postgres  193 Aug  3  2017 .bash_profile
-rw-r--r--  1 postgres postgres  231 Aug  3  2017 .bashrc


# 编辑修改.bash_profile文件,添加以下内容。
[root@weekend02 postgres]# vi .bash_profile 
export PGHOME=/opt/pgsql/postgresql
export PGDATA=$PGHOME/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin

# 保存,退出vi。执行以下命令,使环境变量生效
[root@weekend02 postgres]# source .bash_profile

8、initdb初使化数据库

# 切换用户到postgres并使用initdb初使化数据库
[root@weekend02 postgres]# su - postgres

[postgres@i-vfyxrtmx ~]$ 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/pgsql/postgresql/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 ... PRC
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/pgsql/postgresql/data -l logfile start


# 可以看到 /pgsql/postgresql/data已经有文件了。
[postgres@i-vfyxrtmx ~]$ ls -lh $PGDATA
total 120K
drwx------ 5 postgres postgres 4.0K Mar  7 16:29 base
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 global
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_commit_ts
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_dynshmem
-rw------- 1 postgres postgres 4.7K Mar  7 16:29 pg_hba.conf
-rw------- 1 postgres postgres 1.6K Mar  7 16:29 pg_ident.conf
drwx------ 4 postgres postgres 4.0K Mar  7 16:29 pg_logical
drwx------ 4 postgres postgres 4.0K Mar  7 16:29 pg_multixact
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_notify
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_replslot
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_serial
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_snapshots
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_stat
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_stat_tmp
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_subtrans
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_tblspc
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_twophase
-rw------- 1 postgres postgres    3 Mar  7 16:29 PG_VERSION
drwx------ 3 postgres postgres 4.0K Mar  7 16:29 pg_wal
drwx------ 2 postgres postgres 4.0K Mar  7 16:29 pg_xact
-rw------- 1 postgres postgres   88 Mar  7 16:29 postgresql.auto.conf
-rw------- 1 postgres postgres  29K Mar  7 16:29 postgresql.conf

9、配置服务

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

[postgres@i-vfyxrtmx 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@weekend02 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

10、设置开机自启动

# PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
# linux文件即为linux系统上的启动脚本
[root@i-vfyxrtmx start-scripts]# pwd
/opt/soft/postgresql-14.2/contrib/start-scripts
[root@i-vfyxrtmx start-scripts]# 
[root@i-vfyxrtmx start-scripts]# ls
freebsd  linux  macos

# 1)切换为root用户,修改linux文件属性,添加X属性
[root@i-vfyxrtmx start-scripts]# chmod a+x linux

# 2) 复制linux文件到/etc/init.d目录下,更名为postgresql
[root@i-vfyxrtmx start-scripts]# cp linux /etc/init.d/postgresql

# 3)修改/etc/init.d/postgresql文件的两个变量
prefix设置为postgresql的安装路径(根目录):prefix=/opt/pgsql/postgresql
PGDATA设置为postgresql的数据目录路径:PGDATA="/opt/pgsql/postgresql/data"
普通账户名称:PGUSER=postgres
日志目录:PGLOG="$PGDATA/serverlog"

# 4)设置postgresql服务开机自启动
# centos6.*,Centos7.4 用以下方式
[root@weekend02 init.d]# chkconfig --add postgresql

# 查看开机自启动服务设置成功。
[root@i-vfyxrtmx start-scripts]# chkconfig
......
postgresql     	0:off	1:off	2:on	3:on	4:on	5:on	6:off

# 防火墙设置
# centos7.4的设置
#1. 开启防火墙:没有任何提示,表示开启成功
systemctl status firewalld
systemctl start firewalld
#2. 添加mysql服务到防火墙,并加载
firewall-cmd --permanent --zone=public --add-service=postgresql
firewall-cmd --reload

# centos6.x的设置
# 1)编辑/etc/sysconfig/iptables文件开放5432端口。
[root@weekend02 sysconfig]# cd /etc/sysconfig
[root@weekend02 sysconfig]# vi /etc/sysconfig/iptables
# 添加以下内容, 并重启服务
-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT

[root@weekend02 sysconfig]# /etc/init.d/iptables restart
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:正在卸载模块:                                   [确定]
iptables:应用防火墙规则:                                 [确定]

# 查看端口是否开放
[root@weekend02 sysconfig]# /sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5432 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   
6)执行service postgresql start,启动PostgreSQL服务

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

# 查看PostgreSQL服务
[root@i-vfyxrtmx sysconfig]# ps -ef | grep postgres
root      4654 16032  0 16:29 pts/0    00:00:00 su - postgres
postgres  4655  4654  0 16:29 pts/0    00:00:00 -bash
postgres 11983     1  0 16:50 ?        00:00:00 /opt/pgsql/postgresql/bin/postmaster -D /opt/pgsql/postgresql/data
postgres 11985 11983  0 16:50 ?        00:00:00 postgres: checkpointer 
postgres 11986 11983  0 16:50 ?        00:00:00 postgres: background writer 
postgres 11987 11983  0 16:50 ?        00:00:00 postgres: walwriter 
postgres 11988 11983  0 16:50 ?        00:00:00 postgres: autovacuum launcher 
postgres 11989 11983  0 16:50 ?        00:00:00 postgres: stats collector 
postgres 11990 11983  0 16:50 ?        00:00:00 postgres: logical replication launcher 
postgres 13614 11983  0 16:55 ?        00:00:00 postgres: postgres postgres 10.92.1.2(12814) idle
root     14690  6960  0 16:58 pts/2    00:00:00 grep --color=auto postgres

[root@i-vfyxrtmx sysconfig]# netstat -ltnup | grep post
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      11983/postmaster    
tcp6       0      0 :::5432                 :::*                    LISTEN      11983/postmaster 

11、开始测试

# 切换为postgres用户,进入客户端:
$ su - postgres
$ psql
创建数据库用户
赋予账号权限
新建数据库
退出

[postgres@weekend02 ~]$ psql 
psql (11.1)
Type "help" for help.

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

postgres=# create user pg password 'pg';
CREATE ROLE
postgres=# ALTER ROLE pg SUPERUSER;
ALTER ROLE
postgres=# create database pg;
CREATE DATABASE
postgres=# \q
重新登录数据库
输入密码
显示数据库

[postgres@weekend02 ~]$ psql -U pg -d pg -h i-vfyxrtmx
psql (11.1)
Type "help" for help.

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

pg=# 

说明数据库安装成功。

12、navicat远程测试

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若行若冲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值