PG-部署安装

1、在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。

[root@test pgsql]# ls
postgresql-16.1.tar.gz
[root@test pgsql]# pwd
/pgsql

2、解压压缩包

[root@test pgsql]# tar -zxvf postgresql-16.1.tar.gz 

3、进入解压后的文件夹

[root@test pgsql]# cd postgresql-16.1
[root@test postgresql-16.1]# ls
aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
config      configure.in  COPYRIGHT  GNUmakefile.in  INSTALL  README

4、编译postgresql源码

[root@test postgresql-16.1]# ./configure --prefix=/pgsql/postgresql
选项  描述
–prefix=prefix  安装到prefix指向的目录;默认为/usr/local/pgsql
–bindir=dir 安装应用程序到dir;默认为prefix/bin
–with-docdir=dir    安装文档到dir;默认为prefix/doc
–with-pgport=port   设置默认的服务器端网络连接服务TCP端口号
–with-tcl   为服务端提供Tcl存储过程支持
–with-perl  为服务端提供Perl存储过程支持
–with-python    为服务端提供Python存储过程支持
[root@test postgresql-16.1]# make
[root@test postgresql-16.1]# make install

至此,已完成postgreql的安装。进入/pgsql/postgresql目录可以看到安装后的postgresql的文件。

[root@test postgresql]# ls
bin  include  lib  share

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

[root@test postgresql-16.1]# groupadd postgres
[root@test postgresql-16.1]# useradd -g postgres postgres
[root@test postgresql-16.1]# id postgres
uid=1001(postgres) gid=1001(postgres) 组=1001(postgres)

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

这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql/postgresql/data目录下:

[root@test postgresql-16.1]# cd /pgsql/postgresql
[root@test postgresql]# mkdir data
[root@test postgresql]# chown postgres:postgres data
[root@test postgresql]# ls -al
总用量 28
drwxr-xr-x. 7 root     root     4096 12月 15 23:39 .
drwxr-xr-x. 4 root     root     4096 12月 15 23:14 ..
drwxr-xr-x. 2 root     root     4096 12月 15 23:14 bin
drwxr-xr-x. 2 postgres postgres 4096 12月 15 23:39 data
drwxr-xr-x. 6 root     root     4096 12月 15 23:14 include
drwxr-xr-x. 4 root     root     4096 12月 15 23:14 lib
drwxr-xr-x. 6 root     root     4096 12月 15 23:14 share

七、配置环境变量

进入home/postgres目录可以看到.bash_profile文件。

[root@test postgresql]# cd /home/postgres
[root@test postgres]# ls -al
总用量 28
drwx------. 4 postgres postgres 4096 12月 15 23:37 .
drwxr-xr-x. 5 root     root     4096 12月 15 23:19 ..
-rw-r--r--. 1 postgres postgres   18 5月  11 2012 .bash_logout
-rw-r--r--. 1 postgres postgres  178 12月 15 23:37 .bash_profile
-rw-r--r--. 1 postgres postgres  124 5月  11 2012 .bashrc
drwxr-xr-x. 2 postgres postgres 4096 11月 12 2010 .gnome2
drwxr-xr-x. 4 postgres postgres 4096 9月  29 05:12 .mozilla
[root@test postgres]# 

编辑修改.bash_profile文件。

[root@test postgres]# vi .bash_profile
添加以下内容。

export PGHOME=/pgsql/postgresql

export PGDATA=/pgsql/postgresql/data

PATH=$PATH:$HOME/bin:$PGHOME/bin

保存,退出vi。执行以下命令,使环境变量生效

[root@test postgres]# source .bash_profile 

八、切换用户到postgres并使用initdb初使用化数据库

[root@test postgres]# su - postgres

[postgres@test ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
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 /pgsql/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

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 /pgsql/postgresql/data -l logfile start

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

[postgres@test /]$ cd /pgsql/postgresql/data

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

九、配置服务

修改/pgsql/postgresql/data目录下的两个文件。

postgresql.conf 配置PostgreSQL数据库服务器的相应的参数。

pg_hba.conf 配置对数据库的访问权限。

[postgres@test data]$ vi postgresql.conf 

listen_addresses = '*'                  # 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)
其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。

[postgres@test 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

十、设置PostgreSQL开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

linux文件即为linux系统上的启动脚本

[postgres@test pgsql]$ cd /pgsql/postgresql-16.1/contrib/start-scripts
[postgres@test start-scripts]$ ls
freebsd  linux  macos

1)切换为root用户,修改linux文件属性,添加X属性

[root@test start-scripts]# chmod a+x linux

2) 复制linux文件到/etc/init.d目录下,更名为postgresql

[root@test start-scripts]# cp linux /etc/init.d/postgresql

3)修改/etc/init.d/postgresql文件的两个变量

prefix设置为postgresql的安装路径:/pgsql/postgresql

PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data

4)设置postgresql服务开机自启动

[root@test init.d]# chkconfig --add postgresql

查看开机自启动服务设置成功。

[root@test init.d]# chkconfig

postgresql         0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭

5)编辑/etc/sysconfig/iptables文件开放5432端口。

[root@test sysconfig]# cd /etc/sysconfig
[root@test sysconfig]# vi iptables

添加以下内容

-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT

重启服务

[root@test sysconfig]# /etc/init.d/iptables restart
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter           [确定]
iptables:正在卸载模块:                                     [确定]
iptables:应用防火墙规则:                                 [确定]

查看端口是否开放

[root@test sysconfig]# /sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5432 
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

6)执行service postgresql start,启动PostgreSQL服务

[root@test init.d]# service postgresql start
Starting PostgreSQL: ok

查看PostgreSQL服务

[root@test init.d]# ps -ef | grep postgres
root      12040   3014  0 Dec15 pts/0    00:00:00 su - postgres
postgres  12041  12040  0 Dec15 pts/0    00:00:00 -bash
postgres  12177      1  0 00:29 ?        00:00:00 /pgsql/postgresql/bin/postmaster -D /pgsql/postgresql/data
postgres  12179  12177  0 00:29 ?        00:00:00 postgres: checkpointer                                    
postgres  12180  12177  0 00:29 ?        00:00:00 postgres: background writer                               
postgres  12181  12177  0 00:29 ?        00:00:00 postgres: walwriter                                       
postgres  12182  12177  0 00:29 ?        00:00:00 postgres: autovacuum launcher                             
postgres  12183  12177  0 00:29 ?        00:00:00 postgres: stats collector                                 
postgres  12184  12177  0 00:29 ?        00:00:00 postgres: logical replication launcher                    
root      12198  12132  0 00:30 pts/0    00:00:00 grep postgres

十一、开始测试

切换为postgres用户,进入客户端:

$ su - postgres
$ psql

创建数据库用户

赋予账号权限

新建数据库
退出

[postgres@test ~]$ psql 
psql (16.1)
Type "help" for help.

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres=# create user yq  password '71******';
CREATE ROLE
postgres=# ALTER ROLE yq SUPERUSER;
ALTER ROLE
postgres=# create database t1;
CREATE DATABASE
postgres=# \q

重新登录数据库
输入密码
显示数据库

[postgres@test ~]$ psql -U yq -d postgresql  -h test -p 5432
psql (16.1)
Type "help" for help.

pg=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 t1        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

pg=# 

说明数据库安装成功。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容