centos7下 postgresql-12的安装及卸载方法

一、centos7 postgresql-12的预安装版本的安装方法:

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql12-server

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12

二、postgresql-12 源码安装方式:

1 下载

官网提供了源码和预安装的版本。 源码需要编译安装,解决依赖包等问题,而预安装的版本要简单很多。只需下载解压, 初始化数据库即可。
本例以源码安装为例:请至官网 下载源码。
本例中安装的是PG12. 在 https://www.postgresql.org/ftp/source/v12.0/ 中选择postgresql-12.0.tar.bz2
请根据需要先择自己需要的版本。
预安装版本,请至官网下载。

准备环境

此步骤以root用户执行。 将postgresql的安装包放到/opt 路径。

# 创建组和用户
groupadd postgre
useradd -g postgre -G postgre -d /home/postgresql postgre
passwd postgre
# 安装依赖包
yum install -y bzip2 readline-devel zlib-devel
# 将安装包放在/opt 路径中并解压
cd /opt/
bunzip2 postgresql-12.0.tar.bz2
tar -xvf ./postgresql-12.0.tar

编译安装

此步骤以postgre 用户操作:

cd /opt/postgresql-12.0
./configure --prefix=/home/postgresql/dbhome
make && make install

安装后,可执行文件,库文件等都会安装在/home/postgresql/dbhome 中。

[postgre@boss20 dbhome]$ ls /home/postgresql/dbhome
bin  include  lib  share

设置环境变量

将以下两行添加至postgre 用户的环境变量文件 .bash_profile 并生效。

export LD_LBRARY_PATH=$HOME/dbhome/lib:$LD_LIBRARY_PATH
export PATH=$HOME/dbhome/bin:$PATH

环境变量文件生效方法:

. .bash_profile
或者
source .bash_profile

初始化数据库

此步骤以postgre用户执行。

  • 创建数据存放路径

    mkdir $HOME/data
    

    postgresql 数据库的配置文件,数据文件等都会存放在这个路径下。

  • 初始化数据库

    initdb --locale=C -E UNICODE -D $HOME/data/
    

    示例如下:

      The files belonging to this database system will be owned by user "postgre".
    This user must also own the server process.
    
    The database cluster will be initialized with locale "C".
    The default text search configuration will be set to "english".
    
    Data page checksums are disabled.
    
    fixing permissions on existing directory /home/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 ... America/New_York
    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 /home/postgresql/data/ -l logfile start
    

初始化完成后,在最后一行会提示我们启动数据库的方法. 此时我们来查看一下 $HOME/data路径 的内容:

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

这些路径分别有什么用途,以后再了解。

配置参数文件

我们主要是配置postgresql.conf 和 pg_hba.conf 这两个。

  • postgresql.conf 针对实例的配置
  • pg_hba.conf 针对数据库访问的控制

6.1 postgresql.conf

找到 #port 和 #listener_address 这两个参数。这两个参数是相邻的:

#listen_addresses = 'localhost'               # 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)

将两行行首的 # 删除,将 localhost 改为当前服务器的IP 。 默认的监听端口是5432,可以自行指定另外一个端口号,*不建议使用默认端口号* 。 顺便再修改一下 max_connections 吧,最大的连接数, 100有点少,特别是业务系统。 可以调整成1000,或者更高。

6.2 pg_hba.conf

将以下一行添加至文末。

host    all             all             10.10.100.0/24          trust

其意义是允许 10.10.100网段的IP 连接此服务器上的PG. 如果想允许所有IP 都可以连接此服务器 则可以配置成如下:

host    all             all             0.0.0.0/0          trust

数据库启动与关闭

 

7.1 手动

我们手动启动与关闭数据库是执行pg_ctl命令。在执行时,需要指定 数据路径,格式如下:

pg_ctl -D <数据存放路径> [ stop | start ]

示例如下:

[postgre@boss20 data]$ pg_ctl -D /home/postgresql/data/ -l logfile start
waiting for server to start.... done
server started
[postgre@boss20 data]$ pg_ctl -D $HOME/data stop
waiting for server to shut down.... done
server stopped

7.2 开机自动启动

此步骤需要root用户操作。 postgresql 的安装包中提供了数据库启动与关闭的脚本,可以帮助我们简化操作,也可以 用作开机启动的脚本和service/systemctl 控制服务的脚本。 脚本位于:

<path>/postgresql-12.0/contrib/start-scripts/
本示例中是:
/opt/postgresql-12.0/contrib/start-scripts/

设置开机启动:

cp /opt/postgresql-12.0/contrib/start-scripts/linux /etc/init.d/postgresql
chkconfig --add postgresql
chmod 755 /etc/init.d/postgresql

调整配置,主要是脚本中三个变量的值:

prefix="/home/postgresql/dbhome"
PGDATA="/home/postgresql/data"
PGUSER=postgre
  • prefix 是软件的安装路径
  • PGDATA 是数据存放路径
  • PGUSER 是启动postgresql服务的用户

以后可以开机启动和通过service 命令控制启动和关闭了:

[root@boss20 init.d]# service postgresql start
Starting PostgreSQL: ok
[root@boss20 init.d]# service postgresql stop
Stopping PostgreSQL: ok

卸载方法:
(1)源码方式安装的话:make uninstall。如果你的源代码目录已经删了,也可以重新展开、配置,然后make uninstall。
(2)rpm方式安装的话:rpm -e也就可以了。
(3)yum方式安装的话:yum remove postgresql*也就可以了。

 

三、修改PostgreSQL数据库默认用户postgres的密码

PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下:

步骤一:登录PostgreSQL

1

sudo -u postgres psql

步骤二:修改登录PostgreSQL密码

 ALTER USER postgres WITH PASSWORD 'postgres';

注:

  • 密码postgres要用引号引起来
  • 命令最后有分号

步骤三:退出PostgreSQL客户端

\q

2. 修改linux系统postgres用户的密码

PostgreSQL会创建一个默认的linux用户postgres,修改该用户密码的方法如下:

步骤一:删除用户postgres的密码

sudo passwd -d postgres

步骤二:设置用户postgres的密码

sudo -u postgres passwd

系统提示输入新的密码

1

2

3

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

 

Author: hongyang zhong

Created: 2020-12-19

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值