Linux安装部署PostgreSQL具体步骤

[复制链接]
发表于 2024-7-27 08:13:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
一、版本和情况

选择要安装的版本举行下载:PostgreSQL: File Browser
我选的版本是:postgresql-14.7.tar.gz 操作体系是:CentOS-stream9
二、安装依赖包

在要安装postgresql数据库的Linux服务器(hostname:hdp001)上实行以下命令安装所需要的依赖包:
  1. 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

1、上传安装包并将postgresql-14.7.tar.gz解压,我是将安装包传的目次/home/appview/dev_package,目次自己决定

  1. [root@hdp001 dev_package]# cd /home/appview/dev_package/
  2. [root@hdp001 dev_package]# ll
  3. -rw-r--r-- 1 appview appview  29070900  7月  6 16:55 postgresql-14.7.tar.gz
  4. [root@hdp001 pgsql]# pwd
复制代码
2、解压压缩包

  1. [root@hdp001 pgsql]# tar -zxvf postgresql-14.7.tar.gz
  2. # 解压过程略过了,太多了
复制代码
3、进入解压后的文件夹

  1. [root@hdp001 dev_package]# cd postgresql-14.7.
  2. [root@hdp001 postgresql-14.7.]# ls
  3. aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
  4. config      configure.in  COPYRIGHT  GNUmakefile.in  INSTALL  README
复制代码
4、编译postgresql源码

  1. [root@hdp001 postgresql-14.7.]# ./configure
  2. ​
  3. #############################################################
  4. 这里我选择默认的安装目录所有不指定了,默认为/usr/local/pgsql
  5. -–prefix=prefix 安装到prefix指向的目录;
  6. --without-readline 选项的作用是禁用对Readline库的使用,用于命令行编辑和历史记录
  7. 我在内网安装的时候通常加上这个,不加的话会报错,无法进行编译下面这个
  8. ############################################################
  9. # 以下根据需求自行选择
  10. -–bindir=dir    安装应用程序到dir;默认为prefix/bin
  11. -–with-docdir=dir   安装文档到dir;默认为prefix/doc
  12. -–with-pgport=port  设置默认的服务器端网络连接服务TCP端口号
  13. -–with-tcl  为服务端提供Tcl存储过程支持
  14. -–with-perl 为服务端提供Perl存储过程支持
  15. -–with-python   为服务端提供Python存储过程支持
  16. [root@hdp001 postgresql-14.7.]# make
  17. # 过程略过
  18. [root@hdp001 postgresql-14.7.]# make install
  19. # 过程略过
  20. [root@hdp001 psql]# ls
  21. bin  include  lib  share
复制代码
四、创建用户组postgres并创建用户postgres

  1. [root@hdp001 postgresql-14.7.]# groupadd postgres
  2. [root@hdp001 postgresql-14.7.]# useradd -g postgres postgres
  3. [root@hdp001 postgresql-14.7.]# id postgres
  4. 用户id=1002(postgres) 组id=1002(postgres) 组=1002(postgres)
复制代码
五、创建postgresql数据库的数据主目次并修改文件所有者

这个数据库主目次是随现真相况而不同,这里我们的主目次是在/pgsql/postgresql/data目次下:
  1. [root@hdp001 postgresql-14.7]# cd /usr/local/pgsql/
  2. [root@hdp001 pgsql]# mkdir data
  3. [root@hdp001 pgsql]# chown -R postgres:postgres /usr/local/pgsql
  4. [root@hdp001 pgsql]# ls -al
  5. [root@hdp001 pgsql]# ls -al
  6. 总用量 20
  7. drwxr-xr-x   7 postgres postgres   68  6月 23 00:28 .
  8. drwxr-xr-x. 14 root     root      164  6月 23 00:26 ..
  9. drwxr-xr-x   2 postgres postgres 4096  6月 23 00:26 bin
  10. drwx------  19 postgres postgres 4096  7月  6 16:40 data
  11. drwxr-xr-x   6 postgres postgres 4096  6月 23 00:26 include
  12. drwxr-xr-x   4 postgres postgres 4096  6月 23 00:26 lib
  13. drwxr-xr-x   6 postgres postgres 4096  6月 23 00:26 share
复制代码
六、配置情况变量

进入/home/postgres目次可以看到.bash_profile文件。
  1. [root@hdp001 pgsql]# cd /home/postgres
  2. [root@hdp001 pgsql]# ls -al
  3. [root@hdp001 postgres]# ll -la
  4. 总用量 212
  5. drwx------  4 postgres postgres    165  7月  6 16:39 .
  6. drwxr-xr-x. 5 root     root         50  6月 23 00:28 ..
  7. -rw-------  1 postgres postgres    685  7月  6 16:54 .bash_history
  8. -rw-r--r--  1 postgres postgres     18  2月 15 23:31 .bash_logout
  9. -rw-r--r--  1 postgres postgres    241  6月 23 00:32 .bash_profile
  10. -rw-r--r--  1 postgres postgres    492  2月 15 23:31 .bashrc
  11. drwx------  2 postgres postgres      6  6月 23 00:32 .cache
  12. -rw-------  1 postgres postgres 191649  6月 23 16:12 logfile
  13. drwxr-xr-x  4 postgres postgres     39  5月 30 07:04 .mozilla
  14. -rw-------  1 postgres postgres     84  7月  6 16:46 .psql_history
  15. -rw-------  1 postgres postgres   4016  7月  6 16:39 .viminfo
  16. ​
  17. ########添加环境变量
  18. [root@hdp001 postgres]# vi .bash_profile
  19. 添加以下内容。
  20. export PGHOME=/usr/local/pgsql
  21. export PGDATA=/usr/local/pgsql/data
  22. PATH=$PATH:$HOME/bin:$PGHOME/bin
  23. 保存,退出vi。执行以下命令,使环境变量生效
  24. [root@hdp001 postgres]# source .bash_profile
复制代码
七、切换用户到postgres并使用initdb初使用化数据库

  1. [root@hdp001 postgres]# su - postgres
  2. [postgres@hdp001 ~]$ initdb
  3. The files belonging to this database system will be owned by user "postgres".
  4. This user must also own the server process.
  5. ​
  6. The database cluster will be initialized with locale "zh_CN.UTF-8".
  7. The default database encoding has accordingly been set to "UTF8".
  8. initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
  9. The default text search configuration will be set to "simple".
  10. #####过程略过
  11. Success. You can now start the database server using:
  12. ​
  13.    pg_ctl -D /usr/local/pgsql/data -l logfile start
  14. # 看到这里可以切换到data目录下看看
  15. ##########################################################
  16. [postgres@hdp001 ~]$ cd /usr/local/pgsql/data/
  17. [postgres@hdp001 data]$
  18. [postgres@hdp001 data]$
  19. [postgres@hdp001 data]$ pwd
  20. /usr/local/pgsql/data
  21. [postgres@hdp001 data]$
  22. [postgres@hdp001 data]$ ls
  23. base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
  24. global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
  25. pg_commit_ts  pg_multixact   pg_stat       PG_VERSION
  26. pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal
  27. pg_hba.conf   pg_replslot    pg_subtrans   pg_xact
复制代码
八、配置服务

修改/usr/local/pgsql/data/目次下的两个文件
postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。
pg_hba.conf 配置对数据库的访问权限。
  1. [postgres@hdp001 data]$ vi postgresql.conf
  2. ## 找到以下内容进行修改即可
  3. ##########################################################
  4. listen_addresses = '*'                  # what IP address(es) to listen on;
  5.                                        # comma-separated list of addresses;
  6.                                        # defaults to 'localhost'; use '*' for all
  7.                                        # (change requires restart)
  8. #port = 5432                            # (change requires restart)
  9. ########说明
  10. 其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。
复制代码
  1. [postgres@hdp001 data]$ vim pg_hba.conf
  2. ## 找到以下内容进行修改即可,加入host    all             all             127.0.0.1/32            trust
  3. #################################################################################
  4. ​
  5. # "local" is for Unix domain socket connections only
  6. local   all             all                                     trust  
  7. # IPv4 local connections:
  8. host    all             all             0.0.0.0/0               trust
  9. host    all             all             127.0.0.1/32            trust  ###新加的
  10. # IPv6 local connections:
  11. host    all             all             ::1/128                 trust
  12. # Allow replication connections from localhost, by a user with the
  13. # replication privilege.
  14. local   replication     all                                     trust
  15. host    replication     all             127.0.0.1/32            trust
  16. host    replication     all             ::1/128                 trust
复制代码
阐明:local和IPv4、IPv6后面的trust是当地登录大概远程登录不验证暗码,如果需要暗码验证的把trust改成md5
九、设置PostgreSQL开机自启动

为PostgreSQL的开机自启动脚本,systemctl 的启动文件,一般存放在/etc/systemd/system 大概/usr/lib/systemd/system/文件夹下
我是在/usr/lib/systemd/system/创建一个postgresql.service,并给赋予实行权限,当然开机自启的方式不知这一种
  1. [root@hdp001 system]# cd /usr/lib/systemd/system/
  2. [root@hdp001 system]#
  3. [root@hdp001 system]# vim postgresql.service
  4. [root@hdp001 system]# chmod +x postgresql.service
  5. # 添加以下自动脚本
复制代码
  1. [Unit]
  2. # 服务名称
  3. Description=postgresql.service
  4. # 在network.target服务之后运行
  5. After=network.target
  6. [Service]
  7. # 服务的类型,常用的有 simple(默认类型) 和 forking。
  8. Type=forking
  9. #运行程序的用户和群组
  10. User=postgres
  11. Group=postgres
  12. # 工作目录
  13. WorkingDirectory=/usr/local/pgsql
  14. # 启动命令
  15. ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
  16. # 重启命令
  17. ExecReload=/usr/local/pgsql/bin/pg_ctl restart -D /usr/local/pgsql/data
  18. # 停止命令
  19. ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/bin/data
  20. # 是否给服务分配独立的临时空间,需要
  21. PrivateTmp=true
  22. [Install]
  23. # 列出依赖当前服务的模块
  24. WantedBy=multi-user.target
复制代码
切换到有权限的的用户实行以下命令

  1. ## 重载systemctl服务
  2. [root@hdp001 ~]# systemctl daemon-reload
  3. ## 启动postgresql服务
  4. [root@hdp001 ~]# systemctl start postgresql.service
  5. ## 查看服务状态
  6. [root@hdp001 ~]# systemctl status postgresql.service
  7. ● postgresql.service
  8.     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; preset: disabled)
  9.     Active: active (running) since Sat 2024-07-06 16:40:42 CST; 1h 16min ago
  10.    Process: 2394 ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data (code=exited, status=0/SUCCESS)
  11.   Main PID: 2397 (postgres)
  12.      Tasks: 7 (limit: 48581)
  13.     Memory: 26.0M
  14.        CPU: 570ms
  15.     CGroup: /system.slice/postgresql.service
  16.             ├─2397 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
  17.             ├─2400 "postgres: checkpointer "
  18.             ├─2401 "postgres: background writer "
  19.             ├─2402 "postgres: walwriter "
  20.             ├─2403 "postgres: autovacuum launcher "
  21.             ├─2404 "postgres: stats collector "
  22.             └─2405 "postgres: logical replication launcher "
  23. ​
  24. 7月 06 16:40:42 hdp001 pg_ctl[2397]: 2024-07-06 16:40:42.244 CST [2397] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
  25. 7月 06 16:40:42 hdp001 pg_ctl[2398]: 2024-07-06 16:40:42.249 CST [2398] LOG:  database system was interrupted; last known up at 2024-06-23 16:15:01 CST
  26. 7月 06 16:40:42 hdp001 pg_ctl[2398]: 2024-07-06 16:40:42.279 CST [2398] LOG:  database system was not properly shut down; automatic recovery in progress
  27. 7月 06 16:40:42 hdp001 pg_ctl[2398]: 2024-07-06 16:40:42.280 CST [2398] LOG:  redo starts at 0/1A0C440
  28. 7月 06 16:40:42 hdp001 pg_ctl[2398]: 2024-07-06 16:40:42.281 CST [2398] LOG:  invalid record length at 0/1A0DB10: wanted 24, got 0
  29. 7月 06 16:40:42 hdp001 pg_ctl[2398]: 2024-07-06 16:40:42.281 CST [2398] LOG:  redo done at 0/1A0DAD0 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed:>
  30. 7月 06 16:40:42 hdp001 pg_ctl[2397]: 2024-07-06 16:40:42.284 CST [2397] LOG:  database system is ready to accept connections
  31. 7月 06 16:40:42 hdp001 pg_ctl[2394]:  done
  32. 7月 06 16:40:42 hdp001 pg_ctl[2394]: server started
  33. 7月 06 16:40:42 hdp001 systemd[1]: Started postgresql.service.
  34. [root@hdp001 ~]#
  35. [root@hdp001 ~]#
复制代码
十、开始测试

  1. 切换为postgres用户,进入客户端:
  2. [root@hdp001 system]# su - postgres
  3. [postgres@hdp001 ~]$ psql
  4. psql (14.7)
  5. Type "help" for help.
  6. ​
  7. postgres=#
  8. postgres=# \l
  9.                                  List of databases
  10.   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges  
  11. -----------+----------+----------+-------------+-------------+-----------------------
  12. postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
  13. template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
  14.           |          |          |             |             | postgres=CTc/postgres
  15. template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
  16.           |          |          |             |             | postgres=CTc/postgres
  17. (3 rows)
  18. # 创建用户并设定密码
  19. postgres=#create user hive with password  'Psbc@2025';
  20. CREATE ROLE
  21. # 创建数据库指定属组
  22. postgres=# create database hivemeta_db   owner hive;
  23. CREATE DATABASE
  24. # 授权
  25. postgres=# grant all privileges on  database hivemeta_db   to hive ;
  26. GRANT
  27. postgres=#
  28. #显示数据库
  29. postgres=# \l
  30.                                    List of databases
  31.      Name      |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
  32. ----------------+----------+----------+-------------+-------------+-----------------------
  33. hivemeta_db    | hive     | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =Tc/hive             +
  34.                |          |          |             |             | hive=CTc/hive
  35. postgres       | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
  36. template0      | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
  37.                |          |          |             |             | postgres=CTc/postgres
  38. template1      | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
  39.                |          |          |             |             | postgres=CTc/postgres
  40. (54rows)
  41. # 退出
  42. postgres=# \q
复制代码
看到以上内容阐明数据库已经安装完成了
仅作为个人学习记录分享,后期偶然间继续分享postgres、elasticsearch等数据迁移,也好久没有写博客了,重要是项目比较忙,加班多,没有精神了^~^!!!
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表