PostgreSQL(Linux版本:16.1)

打印 上一主题 下一主题

主题 996|帖子 996|积分 2988

PostgreSQL我进行升级最新版本后,碰到的题目与其他人的很多不一样,大概我不知道他们是怎样解决的也没有说明。我将我怎样安装和所碰到的题目还有解决方法归纳总结,是最全的安装步骤。现在我将我的步骤一步步的教给大家,此中包含我的解决方法。该安装步骤较长,请各位帅哥玉人大佬们耐心看完。

1、进入官网下载Linux对应最新的版本的PostgreSQL。

 PostgreSQL: File Browser 
下载此文件即可(我会将安装包发布到CSDN中,进行下载即可)。
 2、卸载原有的Postgresql。

  1. # (1)查看原有PostgreSQL的状态
  2. service postgresql-12 status
复制代码

  1. # (2)将原有的PostgreSQL服务停止
  2. service postgresql-12 stop
复制代码

  1. # (3)卸载所有的PostgreSQL(注意输入“y”即可)
  2. yum remove postgresql*
复制代码

  1. # (4)验证PosgreSQL是否被删除
  2. rpm -qa | grep postgresql
复制代码


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

  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
复制代码

4、将下载好的PostgreSQL放到对应的目录下即可。

我在“/usr/local”下创建了一个文件夹”pgsql“,如下图所示。

5、解压该文件。

  1. tar -zxvf postgresql-16.1.tar.gz
复制代码

6、进入解压后的文件夹 


7、编译postgresql源码

  1. ./configure --prefix=/pgsql/postgresql
复制代码

(1)如果提示这个错误的话,就是没有找到ICU库。
  1. checking for icu-uc icu-i18n... no
  2. configure: error: ICU library not found
  3. If you have ICU already installed, see config.log for details on the
  4. failure.  It is possible the compiler isn't looking in the proper directory.
  5. Use --without-icu to disable ICU support.
复制代码
(2)输入“icu-config --version”查看是否安装此库。

(3)安装ICU库(输入“y”即可)。
  1. sudo yum install libicu-devel
复制代码

安装完成
(4)然后再次输入刚才的编译命令执行即可。


8、输入“make”和“make install”命令。





进入“cd /pgsql/postgresql”此目录下可以看到安装后的postgresql的文件:

9、创建用户组postgres并创建用户postgres 

  1. # 删除原有的用户组
  2. [root@tssvr1-c1 postgres]# userdel postgres
  3. [root@tssvr1-c1 postgresql-16.1]# ls
  4. aclocal.m4  config  config.log  config.status  configure  configure.ac  contrib  COPYRIGHT  doc  GNUmakefile  GNUmakefile.in  HISTORY  INSTALL  Makefile  meson.build  meson_options.txt  README  src
  5. [root@tssvr1-c1 postgresql-16.1]#
  6. [root@tssvr1-c1 postgresql-16.1]#
  7. [root@tssvr1-c1 postgresql-16.1]#
  8. [root@tssvr1-c1 postgresql-16.1]# groupadd postgres
  9. [root@tssvr1-c1 postgresql-16.1]# useradd -g postgres postgres
  10. [root@tssvr1-c1 postgresql-16.1]# passwd postgres
  11. #设置postgres用户密码为:Zskj@24123
  12. [root@tssvr1-c1 postgresql-16.1]# id postgres
  13. uid=1001(postgres) gid=1001(postgres) 组=1001(postgres)
复制代码

10、创建postgresql数据库的数据主目录并修改文件所有者

  1. [root@tssvr1-c1 local]# cd /pgsql/postgresql/
  2. [root@tssvr1-c1 postgresql]# ls
  3. bin  include  lib  share
  4. [root@tssvr1-c1 postgresql]# mkdir data
  5. [root@tssvr1-c1 postgresql]# chown postgres:postgres data
  6. [root@tssvr1-c1 postgresql]# ls -al
  7. 总用量 20
  8. drwxr-xr-x.  7 root     root       68 12月 20 09:31 .
  9. drwxr-xr-x. 25 root     root     4096 12月 20 09:27 ..
  10. drwxr-xr-x.  2 root     root     4096 12月 20 09:27 bin
  11. drwxr-xr-x.  2 postgres postgres    6 12月 20 09:31 data
  12. drwxr-xr-x.  6 root     root     4096 12月 20 09:27 include
  13. drwxr-xr-x.  4 root     root     4096 12月 20 09:27 lib
  14. drwxr-xr-x.  6 root     root     4096 12月 20 09:27 share
  15. [root@tssvr1-c1 pgsql]#
复制代码
11、配置环境变量

进入home/postgres目录可以看到.bash_profile文件。
  1. [root@tssvr1-c1 postgresql]# cd /home/postgres/
  2. [root@tssvr1-c1 postgres]# ls -al
  3. 总用量 16
  4. drwx------. 6 postgres postgres 142 12月 19 17:31 .
  5. drwxr-xr-x. 4 root     root      56 12月 19 17:11 ..
  6. -rw-------. 1 postgres postgres 159 12月 20 08:50 .bash_history
  7. -rw-r--r--. 1 postgres postgres  18 4月   1 2020 .bash_logout
  8. -rw-r--r--. 1 postgres postgres 273 12月 19 17:31 .bash_profile
  9. -rw-r--r--. 1 postgres postgres 231 4月   1 2020 .bashrc
  10. drwxrwxr-x. 3 postgres postgres  18 12月 19 17:21 .cache
  11. drwxrwxr-x. 3 postgres postgres  18 12月 19 17:21 .config
  12. drwxrwxr-x. 3 postgres postgres  19 12月 19 17:21 .local
  13. drwxr-xr-x. 4 postgres postgres  39 8月   1 11:34 .mozilla
  14. [root@tssvr1-c1 postgres]# vi .bash_profile
复制代码
编辑修改.bash_profile文件(vi .bash_profile)。
  1. # .bash_profile
  2. # Get the aliases and functions
  3. if [ -f ~/.bashrc ]; then
  4.         . ~/.bashrc
  5. fi
  6. # User specific environment and startup programs
  7. export PGHOME=/usr/local/pgsql/postgresql
  8. export PGDATA=/usr/local/pgsql/postgresql/data
  9. PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PGHOME/bin
  10. export PATH
复制代码
保存,退出vi。执行以下命令,使环境变量生效
  1. [root@tssvr1-c1 postgres]# source .bash_profile
复制代码
12、切换用户到postgres并使用initdb初使用化数据库

  1. [root@tssvr1-c1 local]# chown -R postgres:postgres /pgsql
  2. [root@tssvr1-c1 local]#
  3. [root@tssvr1-c1 local]#
  4. [root@tssvr1-c1 local]# su - postgres
  5. 上一次登录:三 12月 20 10:22:13 CST 2023pts/0 上
  6. [postgres@tssvr1-c1 ~]$
  7. [postgres@tssvr1-c1 ~]$
  8. [postgres@tssvr1-c1 ~]$ cd /pgsql/postgresql/
  9. [postgres@tssvr1-c1 postgresql]$ initdb
  10. The files belonging to this database system will be owned by user "postgres".
  11. This user must also own the server process.
  12. The database cluster will be initialized with locale "zh_CN.UTF-8".
  13. The default database encoding has accordingly been set to "UTF8".
  14. initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
  15. The default text search configuration will be set to "simple".
  16. Data page checksums are disabled.
  17. fixing permissions on existing directory /pgsql/postgresql/data ... ok
  18. creating subdirectories ... ok
  19. selecting dynamic shared memory implementation ... posix
  20. selecting default max_connections ... 100
  21. selecting default shared_buffers ... 128MB
  22. selecting default time zone ... Asia/Shanghai
  23. creating configuration files ... ok
  24. running bootstrap script ... ok
  25. performing post-bootstrap initialization ... ok
  26. syncing data to disk ...
复制代码

可以看到 /pgsql/postgresql/data已经有文件了。

13、配置服务

(1)修改/pgsql/postgresql/data目录下的两个文件。
postgresql.conf   配置PostgreSQL数据库服务器的相应的参数。  
pg_hba.conf        配置对数据库的访问权限。
  1. [postgres@tssvr1-c1 data]$ vi postgresql.conf
  2. listen_addresses = '*'                  # what IP address(es) to listen on;
  3.                                         # comma-separated list of addresses;
  4.                                         # defaults to 'localhost'; use '*' for all
  5.                                         # (change requires restart)
  6. #port = 5432                            # (change requires restart)
复制代码
此中,参数“listen_addresses”表现监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只担当来自本机localhost的毗连请求,这会让远程的主机无法登岸这台数据库,如果想从其他的机器上登岸这台数据库,需要把监听地址改为现实网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表现在本地的所有地址上监听。
把“license_addresses = '*'”放开即可。

(2)
  1. [postgres@tssvr1-c1 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
如下图所示:

14、设置PostgreSQL开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
linux文件即为linux体系上的启动脚本
  1. [postgres@tssvr1-c1 data]$ cd /usr/local/postgresql/postgresql-16.1/contrib/start-scripts/
  2. [postgres@tssvr1-c1 start-scripts]$ ls
  3. freebsd  linux  macos
  4. [postgres@tssvr1-c1 start-scripts]$
  5. [postgres@tssvr1-c1 start-scripts]$
  6. [postgres@tssvr1-c1 start-scripts]$
  7. [postgres@tssvr1-c1 start-scripts]$ ll
  8. 总用量 8
  9. -rw-r--r--. 1 1107 1107 1441 11月  7 06:04 freebsd
  10. -rw-r--r--. 1 1107 1107 3526 11月  7 06:04 linux
  11. drwxrwxrwx. 2 1107 1107   84 11月  7 06:17 macos
  12. [postgres@tssvr1-c1 start-scripts]$
复制代码


  • 切换为root用户,修改linux文件属性,添加X属性
  1. [postgres@tssvr1-c1 start-scripts]$ exit
  2. 登出
  3. [root@tssvr1-c1 local]# cd /usr/local/postgresql/postgresql-16.1/contrib/start-scripts/
  4. [root@tssvr1-c1 start-scripts]# chmod a+x linux
  5. [root@tssvr1-c1 start-scripts]# ls
  6. freebsd  linux  macos
  7. [root@tssvr1-c1 start-scripts]#
  8. [root@tssvr1-c1 start-scripts]# ll
  9. 总用量 8
  10. -rw-r--r--. 1 1107 1107 1441 11月  7 06:04 freebsd
  11. -rwxr-xr-x. 1 1107 1107 3526 11月  7 06:04 linux
  12. drwxrwxrwx. 2 1107 1107   84 11月  7 06:17 macos
复制代码


  • 复制linux文件到/etc/init.d目录下,更名为postgresql
  1. [root@tssvr1-c1 start-scripts]# cp linux /etc/init.d/postgresql
复制代码


  • 修改/etc/init.d/postgresql文件的两个变量
    prefix设置为postgresql的安装路径:/pgsql/postgresql
    PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data
  1. [root@tssvr1-c1 start-scripts]# cd /etc/init.d/
  2. [root@tssvr1-c1 init.d]# ls
  3. functions  netconsole  network  postgresql  README
  4. [root@tssvr1-c1 init.d]#
  5. [root@tssvr1-c1 init.d]# vi postgresql
  6. [root@tssvr1-c1 init.d]#
  7. [root@tssvr1-c1 init.d]#
复制代码



  • 设置postgresql服务开机自启动
  1. [root@tssvr1-c1 init.d]# chkconfig --add postgresql
  2. [root@tssvr1-c1 init.d]#
复制代码
查看开机自启动服务设置成功。
  1. [root@tssvr1-c1 init.d]# chkconfig
  2. 注:该输出结果只显示 SysV 服务,并不包含
  3. 原生 systemd 服务。SysV 配置数据
  4. 可能被原生 systemd 配置覆盖。
  5.       要列出 systemd 服务,请执行 'systemctl list-unit-files'。
  6.       查看在具体 target 启用的服务请执行
  7.       'systemctl list-dependencies [target]'。
  8. netconsole             0:关        1:关        2:关        3:关        4:关        5:关        6:关
  9. network                0:关        1:关        2:开        3:开        4:开        5:开        6:关
  10. postgresql             0:关        1:关        2:开        3:开        4:开        5:开        6:关
  11. [root@tssvr1-c1 init.d]#
复制代码


  • 编辑/etc/sysconfig/iptables文件开放5432端口。
  1. [root@tssvr1-c1 init.d]# cd /etc/sysconfig/
  2. [root@tssvr1-c1 sysconfig]# vi iptables
  3. [root@tssvr1-c1 sysconfig]#
复制代码
添加“-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT”。如有则不消添加



  • 重启服务
  1. vi iptables
  2. [root@tssvr1-c1 sysconfig]# /etc/init.d/iptables restart
  3. -bash: /etc/init.d/iptables: 没有那个文件或目录
  4. [root@tssvr1-c1 sysconfig]#
  5. [root@tssvr1-c1 sysconfig]#
  6. [root@tssvr1-c1 sysconfig]#
  7. [root@tssvr1-c1 sysconfig]#
  8. [root@tssvr1-c1 sysconfig]# systemctl restart iptables
  9. [root@tssvr1-c1 sysconfig]#
  10. [root@tssvr1-c1 sysconfig]# systemctl status iptables
  11. ● iptables.service - IPv4 firewall with iptables
  12.    Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
  13.    Active: active (exited) since 三 2023-12-20 15:03:15 CST; 13s ago
  14.   Process: 14804 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
  15. Main PID: 14804 (code=exited, status=0/SUCCESS)
  16. 12月 20 15:03:15 tssvr1-c1 systemd[1]: Starting IPv4 firewall with iptables...
  17. 12月 20 15:03:15 tssvr1-c1 iptables.init[14804]: iptables: Applying firewall rules: [  确定  ]
  18. 12月 20 15:03:15 tssvr1-c1 systemd[1]: Started IPv4 firewall with iptables.
复制代码


  • 查看端口是否开放
  1. [root@tssvr1-c1 sysconfig]# /sbin/iptables -L -n
  2. Chain INPUT (policy DROP)
  3. target     prot opt source               destination         
  4. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
  5. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
  6. ACCEPT     all  --  192.168.0.0          0.0.0.0/0           
  7. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
  8. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3000
  9. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8060
  10. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8848
  11. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8090
  12. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:48089
  13. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:5432
  14. Chain FORWARD (policy DROP)
  15. target     prot opt source               destination         
  16. Chain OUTPUT (policy DROP)
  17. target     prot opt source               destination         
  18. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
  19. ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
  20. ACCEPT     all  --  0.0.0.0/0            192.168.0.0         
  21. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
  22. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:3000
  23. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8060
  24. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8848
  25. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:8090
  26. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:48089
  27. ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp spt:5432
复制代码

15、启动PostgreSQL服务



  • 执行service postgresql start,启动PostgreSQL服务
  1. [root@tssvr1-c1 sysconfig]# service postgresql start
  2. Starting PostgreSQL: ok
复制代码


  • 查看PostgreSQL服务
  1. [root@tssvr1-c1 sysconfig]# ps -ef | grep postgres
  2. postgres 17727     1  0 15:10 ?        00:00:00 /pgsql/postgresql/bin/postgres -D /pgsql/postgresql/data
  3. postgres 17734 17727  0 15:10 ?        00:00:00 postgres: checkpointer
  4. postgres 17735 17727  0 15:10 ?        00:00:00 postgres: background writer
  5. postgres 17737 17727  0 15:10 ?        00:00:00 postgres: walwriter
  6. postgres 17738 17727  0 15:10 ?        00:00:00 postgres: autovacuum launcher
  7. postgres 17739 17727  0 15:10 ?        00:00:00 postgres: logical replication launcher
  8. root     18365 22465  0 15:11 pts/0    00:00:00 grep --color=auto postgres
复制代码
16、开始测试

  1. [root@tssvr1-c1 postgres]# su postgres
  2. [postgres@tssvr1-c1 ~]$ psql
  3. psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: 没有那个文件或目录
  4.         Is the server running locally and accepting connections on that socket?
  5. [postgres@tssvr1-c1 ~]$ exit
  6. exit
  7. [root@tssvr1-c1 postgres]#
  8. [root@tssvr1-c1 postgres]#
  9. [root@tssvr1-c1 postgres]# service postgresql status
  10. pg_ctl: no server running
  11. [root@tssvr1-c1 postgres]#
  12. [root@tssvr1-c1 postgres]#
  13. [root@tssvr1-c1 postgres]# service postgresql start
  14. Starting PostgreSQL: ok
  15. [root@tssvr1-c1 postgres]#
  16. [root@tssvr1-c1 postgres]#
  17. [root@tssvr1-c1 postgres]#
  18. [root@tssvr1-c1 postgres]# su postgres
  19. [postgres@tssvr1-c1 ~]$
  20. [postgres@tssvr1-c1 ~]$
  21. [postgres@tssvr1-c1 ~]$ psql
  22. psql (16.1)
  23. Type "help" for help.
  24. postgres=#
  25. postgres=#
  26. postgres=#
  27. postgres=#
复制代码

运行此命令使其生效:
  1. source .bash_profile
复制代码

  1. source ~/.bashrc
复制代码


  1. 创建数据库用户
  2. postgres=# create user postgres password 'postgres';
  3. CREATE ROLE
  4. 赋予账号权限
  5. postgres=# ALTER ROLE pg SUPERUSER;
  6. ALTER ROLE
  7. 新建数据库
  8. postgres=# create database pg;
  9. CREATE DATABASE
  10. 退出
  11. postgres=# \q
复制代码
修改“postgres”用户的暗码为“xxx”,然后输入“\q”和“exit”退出,重新输入“”进入即可:
ALTER USER postgres WITH PASSWORD 'postgres';
  1. [postgres@tssvr1-c1 ~]$ psql -U postgres -d postgres -h localhost
  2. psql (16.1)
  3. Type "help" for help.
  4. postgres=# \l
  5.                                                        List of databases
  6.    Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
  7. -----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
  8. postgres  | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           |
  9. template0 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
  10.            |          |          |                 |             |             |            |           | postgres=CTc/postgres
  11. template1 | postgres | UTF8     | libc            | zh_CN.UTF-8 | zh_CN.UTF-8 |            |           | =c/postgres          +
  12.            |          |          |                 |             |             |            |           | postgres=CTc/postgres
  13. (3 rows)
  14. postgres=#
  15. postgres=#
复制代码
毗连“xx.xx.xx.xx”试试,若不行则启动psql服务即可。
  1. [root@tssvr1-c1 sysconfig]# service postgresql start
  2. Starting PostgreSQL: ok[root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# su postgres[postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ psql -U postgres -d postgres -h localhostpsql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  the database system is starting up[postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ psqlpsql (16.1)Type "help" for help.postgres=# postgres=# postgres=# \q[postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ eixbash: eix: 未找到命令...[postgres@tssvr1-c1 sysconfig]$ exitexit[root@tssvr1-c1 sysconfig]# service postgresql statuspg_ctl: server is running (PID: 27800)/pgsql/postgresql/bin/postgres "-D" "/pgsql/postgresql/data"[root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# [root@tssvr1-c1 sysconfig]# su postgres[postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ psql -U postgres -d postgres -h localhostpsql (16.1)Type "help" for help.postgres=# postgres=# postgres=# postgres=# \1invalid command \1Try \? for help.postgres=# \q[postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ [postgres@tssvr1-c1 sysconfig]$ psql -U postgres -d postgres -h 192.168.1.24Password for user postgres: psql (16.1)Type "help" for help.postgres=# postgres=#
复制代码
如许就可以用Navicat进行毗连数据库了。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宝塔山

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表