centOS7安装postgresql12.2

参考:http://www.postgres.cn/v2/download

  • 用户db01-安装postgresql12.2

[db01@centos1 ~]$ pwd
/home/db01
[db01@centos1 ~]$ ll
-rw-r--r--. 1 db01 db01 20363545 2月  10 2021 postgresql-12.2.tar.bz2 #下载到的安装包
[db01@centos1 ~]$ tar xjvf postgresql*.bz2 #解压
[db01@centos1 ~]$ ll
drwxrwxr-x. 6 db01 db01      273 2月  11 2020 postgresql-12.2
-rw-r--r--. 1 db01 db01 20363545 2月  10 2021 postgresql-12.2.tar.bz2
[db01@centos1 ~]$ cd postgresql-12.2/
[db01@centos1 postgresql-12.2]$ ll
总用量 744
-rw-r--r--.  1 db01 db01    522 2月  11 2020 aclocal.m4
drwxrwxr-x.  2 db01 db01   4096 2月  11 2020 config
-rwxr-xr-x.  1 db01 db01 572134 2月  11 2020 configure
-rw-r--r--.  1 db01 db01  83465 2月  11 2020 configure.in
drwxrwxr-x. 56 db01 db01   4096 2月  11 2020 contrib
-rw-r--r--.  1 db01 db01   1192 2月  11 2020 COPYRIGHT
drwxrwxr-x.  3 db01 db01     87 2月  11 2020 doc
-rw-r--r--.  1 db01 db01   3909 2月  11 2020 GNUmakefile.in
-rw-r--r--.  1 db01 db01    284 2月  11 2020 HISTORY
-rw-r--r--.  1 db01 db01  61606 2月  11 2020 INSTALL
-rw-r--r--.  1 db01 db01   1665 2月  11 2020 Makefile
-rw-r--r--.  1 db01 db01   1212 2月  11 2020 README
drwxrwxr-x. 16 db01 db01   4096 2月  11 2020 src
[db01@centos1 postgresql-12.2]$ pwd
/home/db01/postgresql-12.2
[db01@centos1 postgresql-12.2]$ sudo yum install readline-devel #安装依赖包readline-devel
[db01@centos1 postgresql-12.2]$ sudo yum -y install zlib-devel #安装依赖包zlib-devel
[db01@centos1 postgresql-12.2]$ ./configure --prefix=/home/db01/pgsql #configure 指定安装路径
[db01@centos1 postgresql-12.2]$ make world #编译
。。。。。。
PostgreSQL, contrib, and documentation successfully made. Ready to install.
[db01@centos1 postgresql-12.2]$ make install-world #安装
。。。。。。
PostgreSQL, contrib, and documentation installation complete.
[db01@centos1 postgresql-12.2]$ cd .. 
[db01@centos1 ~]$ ll
总用量 19892
drwxrwxr-x. 6 db01 db01       56 2月  10 19:44 pgsql
drwxrwxr-x. 6 db01 db01     4096 2月  10 19:31 postgresql-12.2
-rw-r--r--. 1 db01 db01 20363545 2月  10 2021 postgresql-12.2.tar.bz2
[db01@centos1 ~]$ rm -rf postgresql-12.2 #删掉解压出的安装包
[db01@centos1 ~]$ cd pgsql/
[db01@centos1 pgsql]$ ll
总用量 16
drwxrwxr-x. 2 db01 db01 4096 2月  10 19:44 bin
drwxrwxr-x. 6 db01 db01 4096 2月  10 19:44 include
drwxrwxr-x. 4 db01 db01 4096 2月  10 19:44 lib
drwxrwxr-x. 8 db01 db01 4096 2月  10 19:44 share
[db01@centos1 pgsql]$ mkdir data #建数据存放目录
[db01@centos1 pgsql]$ mkdir log #建日志存放目录
[db01@centos1 pgsql]$ cd data/
[db01@centos1 data]$ pwd
/home/db01/pgsql/data
[db01@centos1 data]$ cd
[db01@centos1 ~]$ ~/pgsql/bin/initdb -D /home/db01/pgsql/data #初始化数据库,指定数据目录
The files belonging to this database system will be owned by user "db01".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /home/db01/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 ... Asia/Shanghai
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:

    /home/db01/pgsql/bin/pg_ctl -D /home/db01/pgsql/data -l logfile start

[db01@centos1 ~]$ /home/db01/pgsql/bin/pg_ctl -D /home/db01/pgsql/data -l /home/db01/pgsql/log/log.log start #启动数据库
waiting for server to start.... done
server started
[db01@centos1 ~]$ /home/db01/pgsql/bin/pg_ctl status -D /home/db01/pgsql/data #查看数据库是否在指定的数据目录运行
pg_ctl: server is running (PID: 67624)
/home/db01/pgsql/bin/postgres "-D" "/home/db01/pgsql/data"
[db01@centos1 ~]$ 

用户db01-修改为md5认证方式

[db01@centos1 ~]$ cd pgsql/data/
[db01@centos1 data]$ sudo vi pg_hba.conf
。。。。。。
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5 # trust改为md5
。。。。。。
[db01@centos1 data]$
#trust:凡是能连接到服务器的,都是可信任的。只需要提供数据库用户名,可以没有对应的操作系统同名用户;
#password 和 md5:对于外部访问,需要提供 psql 用户名和密码。对于本地连接,提供 psql 用户名密码之外,还需要有操作系统访问权(用操作系统同名用户验证)。password 和 md5 的区别就是外部访问时传输的密码是否用 md5 加密;
#ident:对于外部访问,从 ident 服务器获得客户端操作系统用户名,然后把操作系统作为数据库用户名进行登录;对于本地连接,实际上使用了peer;
#peer:通过客户端操作系统内核来获取当前系统登录的用户名,并作为psql用户名进行登录。
  • 用户db01-允许远程登录

[db01@centos1 data]$ cp postgresql.conf postgresql.conf.bak 
[db01@centos1 data]$ cat postgresql.conf.bak |grep -Ev '#|^$'>postgresql.conf
[db01@centos1 data]$ vi postgresql.conf
listen_addresses = '*' #增加1行
[db01@centos1 data]$vi pg_hba.conf
host all all 0.0.0.0/0 password #增加1行
[db01@centos1 data]$
  • 用户db01-重启

[db01@centos1 bin]$ pwd
/home/db01/pgsql/bin
[db01@centos1 bin]$ ./pg_ctl stop -D  /home/db01/pgsql/data
waiting for server to shut down.... done
server stopped
[db01@centos1 bin]$ /home/db01/pgsql/bin/pg_ctl -D /home/db01/pgsql/data -l /home/db01/pgsql/log/log.log start
  • 用户db01-解决其他主机无法登录问题

    [db01@centos1 bin]$ /home/db01/pgsql/bin/psql -h 127.0.0.1 -p 5432 -U db01 -d thingsboard #这样不能登录
    Password for user db01: 
    psql: error: could not connect to server: FATAL:  password authentication failed for user "db01"
    [db01@centos1 bin]$ /home/db01/pgsql/bin/psql -U db01 -d thingsboard  #这样才能登录
    psql (12.2)
    Type "help" for help.
    
    thingsboard=# \password #修改密码,使密码用上md5,这样一来,其他主机也能登录了
    Enter new password: 
    Enter it again: 
    thingsboard=# exit
    [db01@centos1 bin]$

猜你喜欢

转载自blog.csdn.net/mnbwz/article/details/113796159