程序员的知识教程库

网站首页 > 教程分享 正文

PostgreSQL安装与配置(postgresql安装教程)

henian88 2024-10-22 11:49:38 教程分享 7 ℃ 0 评论

1.通过二进制安装包安装 centos 7

通过这个  https://www.postgresql.org/download 
选择对应的版本,就可以获得相应的安装方法,官网支持很强大。

Centos 7版本安装如下
安装PostgreSQL yum仓库
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

安装PostgreSQL:
sudo yum install -y postgresql13-server

安装扩展模块
sudo yum install -y postgresql13-contrib.x86_64

初始化数据库,设置开机启动
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13

在Centos或Redhat下,默认安装上的PostgreSQL的数据目录在/var/lib/pgsql/13/data/
[root@node1 ~]# ls -l /var/lib/pgsql/13/data/
total 64
drwx------ 5 postgres postgres    38 Feb 27 01:25 base
-rw------- 1 postgres postgres    30 Feb 27 01:25 current_logfiles
drwx------ 2 postgres postgres  4096 Feb 27 01:26 global
drwx------ 2 postgres postgres    31 Feb 27 01:25 log
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_commit_ts
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_dynshmem
-rw------- 1 postgres postgres  4548 Feb 27 01:25 pg_hba.conf
-rw------- 1 postgres postgres  1636 Feb 27 01:25 pg_ident.conf
drwx------ 4 postgres postgres    65 Feb 27 03:25 pg_logical
drwx------ 4 postgres postgres    34 Feb 27 01:25 pg_multixact
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_notify
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_replslot
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_serial
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_snapshots
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_stat
drwx------ 2 postgres postgres    60 Feb 27 03:40 pg_stat_tmp
drwx------ 2 postgres postgres    17 Feb 27 01:25 pg_subtrans
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_tblspc
drwx------ 2 postgres postgres     6 Feb 27 01:25 pg_twophase
-rw------- 1 postgres postgres     3 Feb 27 01:25 PG_VERSION
drwx------ 3 postgres postgres    58 Feb 27 01:25 pg_wal
drwx------ 2 postgres postgres    17 Feb 27 01:25 pg_xact
-rw------- 1 postgres postgres    88 Feb 27 01:25 postgresql.auto.conf
-rw------- 1 postgres postgres 28017 Feb 27 01:25 postgresql.conf
-rw------- 1 postgres postgres    58 Feb 27 01:25 postmaster.opts
-rw------- 1 postgres postgres   103 Feb 27 01:25 postmaster.pid

2.从源码安装

# 创建组和用户
groupadd postgres
useradd -g postgres -G postgres -d /home/postgres postgres
passwd postgre

1.下载源代码
https://www.postgresql.org/  
--> Download 
--> Source 
--> v12.6 
--> choose "postgresql-12.6.tar.bz2"
wget https://ftp.postgresql.org/pub/source/v12.6/postgresql-12.6.tar.bz2

2.安装依赖包
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 

3.解压安装包
tar xvf postgresql-12.6.tar.bz2

4.编译安装
cd postgresql-12.6
./configure --prefix=/usr/local/pgsql12.6 --with-segsize=16 --with-blocksize=32 --with-libedit-preferred --with-perl --with-python --with-openssl --with-libxml --with-libxslt --enable-thread-safety --enable-nls=en_US.UTF-8

说明:
--with-perl: 可以使用perl语法的PL/Perl过程语言写自定义函数
--with-python: 可以使用python语法的PL/Python过程语言自定义函数
--enable-thread-safety: 开启线程安全
--with-blocksize=32: 指定数据块32KB

编译安装
make && make install

5.安装contrib目录下的工具
cd postgresql-12.6/contrib
make && make install

6.创建软链接
cd /usr/local
sudo ln -sf /usr/local/pgsql12.6 /usr/local/pgsql
如果12.7发布了,在编译PostgreSQL12.7之后,只需把现有的数据库停掉,然后把链接/usr/local/pgsql指向新版本/usr/local/pgsql12.7即可完成升级。

chown -R postgres:postgres /usr/local/pgsql12.6
chown -R postgres:postgres /usr/local/pgsql
7.安装后的配置
su - postgres
添加到postgre的配置文件.bash_profile
export PATH=$PATH:/usr/local/pgsql/bin
export LD_LIBRARY_PATH=/usr/local/pgsql/lib
export PGDATA=/home/postgre/pgdata
生效变量
source .bash_profile

8.创建数据库簇
mkdir -p $PGDATA
initdb --locale=C -E UNICODE -D $HOME/pgdata/

9.启动和停止数据库
pg_ctl start -D $PGDATA
pg_ctl stop -D $PGDATA -m fast
说明:
fast : 快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库
immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行恢复。

10. 修改postgresql 监听端口5555
echo "listen_addresses = 'localhost'" >> /home/postgre/pgdata/postgresql.conf
echo "port = 5555" >> /home/postgre/pgdata/postgresql.conf
echo "unix_socket_directories = '/tmp'" >> /home/postgre/pgdata/postgresql.conf
修改后,重启postgresql生效。

11. 登陆验证数据库
[postgre@node1 pgdata]$ psql postgres
postgres=# \l
                           List of databases
   Name    |  Owner  | Encoding | Collate | Ctype |  Access privileges
-----------+---------+----------+---------+-------+---------------------
 postgres  | postgre | UTF8     | C       | C     |
 template0 | postgre | UTF8     | C       | C     | =c/postgre         +
           |         |          |         |       | postgre=CTc/postgre
 template1 | postgre | UTF8     | C       | C     | =c/postgre         +
           |         |          |         |       | postgre=CTc/postgre
(3 rows)
这里如果不是postgres用户,就需要指定psql postgres ,否则会报错
2021-02-27 05:48:05.856 GMT [11808] FATAL:  database "postgre" does not exist
psql: error: FATAL:  database "postgre" does not exist
[postgre@node1 pgdata]$ [11808] FATAL:  database "postgre" does not exist

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表