欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

postgresql数据库部署

程序员文章站 2022-05-30 18:51:40
运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 一、postgresql数据库部署 1、前往postgresql安装包的目录(这里我部署的是10.5的版本) 2、解压缩包 3、进入解压后生成的目录文件进行编译 4、提示如下信息是缺少包文件 5、安装相应的包文件文件 6、安装完成后 ......

运维开发技术交流群欢迎大家加入一起学习(qq:722381733)

一、postgresql数据库部署

  1、前往postgresql安装包的目录(这里我部署的是10.5的版本)

[root@web1 ~]# cd /package/
[root@web1 package]# ls
apache-tomcat-8.5.39.tar.gz  jdk-8u131-linux-x64.tar.gz  postgresql-10.5.tar.gz  redis-3.2.0.tar.gz
[root@web1 package]# 

  2、解压缩包

[root@web1 package]# tar xf postgresql-10.5.tar.gz 
[root@web1 package]# ls
apache-tomcat-8.5.39.tar.gz  jdk-8u131-linux-x64.tar.gz  postgresql-10.5  postgresql-10.5.tar.gz  redis-3.2.0.tar.gz
[root@web1 package]# 

  3、进入解压后生成的目录文件进行编译

[root@web1 postgresql-10.5]# ./configure --prefix=/usr/local/pgsql-10.5
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether nls is wanted... no
checking for default port number... 5432
checking for block size... 8kb
checking for segment size... 1gb
checking for wal block size... 8kb
checking for wal segment size... 16mb

  4、提示如下信息是缺少包文件

checking for library containing readline... no
configure: error: readline library not found
if you have readline already installed, see config.log for details on the
failure.  it is possible the compiler isn't looking in the proper directory.
use --without-readline to disable readline support.

  5、安装相应的包文件文件

[root@web1 postgresql-10.5]#yum install ncurses* readline* zlib* -y
######################################
[root@web1 postgresql-10.5]# rpm -aq ncurses* readline* zlib*
readline-devel-6.2-10.el7.x86_64
zlib-static-1.2.7-18.el7.x86_64
ncurses-libs-5.9-14.20130511.el7_4.x86_64
ncurses-devel-5.9-14.20130511.el7_4.x86_64
readline-static-6.2-10.el7.x86_64
zlib-devel-1.2.7-18.el7.x86_64
readline-6.2-10.el7.x86_64
ncurses-base-5.9-14.20130511.el7_4.noarch
ncurses-5.9-14.20130511.el7_4.x86_64
zlib-1.2.7-18.el7.x86_64
[root@web1 postgresql-10.5]#

  6、安装完成后重新编译下

[root@web1 postgresql-10.5]# ./configure --prefix=/usr/local/pgsql-10.5
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether nls is wanted... no
checking for default port number... 5432
checking for block size... 8kb
checking for segment size... 1gb
checking for wal block size... 8kb
checking for wal segment size... 16mb
checking for gcc... gcc

  7、解析文件(在postgresql-10.5目录执行:make install),最后输出结果没提示异常【error】便可开始安装了

rm -f pg_regress.o && ln -s ../../../src/test/regress/pg_regress.o .
gcc -wall -wmissing-prototypes -wpointer-arith -wdeclaration-after-statement -wendif-labels -wmissing-format-attribute -wformat-security -wl,--as-needed -wl,-rpath,'/usr/local/pgsql-10.5/lib',--enable-new-dtags  -lpgcommon -lpgport -lpthread -lz -lreadline -lrt -lcrypt -ldl
make[2]: leaving directory `/package/postgresql-10.5/src/test/isolation'
make -c test/perl all
make[2]: entering directory `/package/postgresql-10.5/src/test/perl'
make[2]: nothing to be done for `all'.
make[2]: leaving directory `/package/postgresql-10.5/src/test/perl'
make[1]: leaving directory `/package/postgresql-10.5/src'
make -c config all
make[1]: entering directory `/package/postgresql-10.5/config'
make[1]: nothing to be done for `all'.
make[1]: leaving directory `/package/postgresql-10.5/config'
all of postgresql successfully made. ready to install.

 

  8、开始安装(在postgresql-10.5目录执行:make install),最后输出结果没提示异常【error】便表示安装成功

/bin/install -c -m 644 makefile.global '/usr/local/pgsql-10.5/lib/pgxs/src/makefile.global'
/bin/install -c -m 644 makefile.port '/usr/local/pgsql-10.5/lib/pgxs/src/makefile.port'
/bin/install -c -m 644 ./makefile.shlib '/usr/local/pgsql-10.5/lib/pgxs/src/makefile.shlib'
/bin/install -c -m 644 ./nls-global.mk '/usr/local/pgsql-10.5/lib/pgxs/src/nls-global.mk'
make[1]: leaving directory `/package/postgresql-10.5/src'
make -c config install
make[1]: entering directory `/package/postgresql-10.5/config'
/bin/mkdir -p '/usr/local/pgsql-10.5/lib/pgxs/config'
/bin/install -c -m 755 ./install-sh '/usr/local/pgsql-10.5/lib/pgxs/config/install-sh'
/bin/install -c -m 755 ./missing '/usr/local/pgsql-10.5/lib/pgxs/config/missing'
make[1]: leaving directory `/package/postgresql-10.5/config'
postgresql installation complete.

  9、安装完成后可以看到在/usr/local目录上成了pgsql-10.5目录

[root@web1 postgresql-10.5]# ll /usr/local/ |grep pgsql-10.5
drwxr-xr-x  6 root root   56 may 23 18:06 pgsql-10.5
[root@web1 postgresql-10.5]# 

  10、创建用户及设置/usr/local/pgsql-10.5目录文件属主组

 

[root@web1 postgresql-10.5]# useradd postgres
[root@web1 postgresql-10.5]# chown -r postgres.postgres /usr/local/pgsql-10.5/
[root@web1 postgresql-10.5]# ll /usr/local/ |grep pgsql-10.5
drwxr-xr-x  6 postgres postgres   56 may 23 18:06 pgsql-10.5
[root@web1 postgresql-10.5]# 

 

  11、给/usr/local/pgsql-10.5目录设置软连接(方便查看和管理吧....)

[root@web1 postgresql-10.5]# ln -s /usr/local/pgsql-10.5/ /usr/local/pgsql
[root@web1 postgresql-10.5]# ll /usr/local/pgsql
lrwxrwxrwx 1 root root 22 may 23 18:15 /usr/local/pgsql -> /usr/local/pgsql-10.5/
[root@web1 postgresql-10.5]# 

  12、进入刚创建的postgres用户

[root@web1 postgresql-10.5]# su postgres
[postgres@web1 postgresql-10.5]$ 

  13、设置环境变量(注意路径)

[postgres@web1 ~]$ vim ~/.bash_profile

# .bash_profile

# get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# user specific environment and startup programs

path=$path:$home/.local/bin:$home/bin
export path=$path:/usr/local/pgsql-10.5/bin
pgdata=/usr/local/pgsql-10.5/data
export pgdata
export path

  14、当前生效

[postgres@web1 ~]$ source ~/.bash_profile 
[postgres@web1 ~]$ 

  15、测试

[postgres@web1 ~]$ which psql
/usr/local/pgsql-10.5/bin/psql
[postgres@web1 ~]$ psql -v
psql (postgresql) 10.5

  16、初始化数据库(initdb --help查看讲情),指定库文件路径

[postgres@web1 ~]$ initdb /usr/local/pgsql-10.5/data
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 "en_us.utf-8".
the default database encoding has accordingly been set to "utf8".
the default text search configuration will be set to "english".

data page checksums are disabled.

creating directory /usr/local/pgsql-10.5/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 /usr/local/pgsql-10.5/data -l logfile start

[postgres@web1 ~]$ 

***在此就已经部署成功了,现在要做的就是修改下配置文件,优化下***

  17、前往文件安装所在地,修改pg_hba.conf配置文件

[postgres@web1 data]$ cp pg_hba.conf pg_hba.conf.`date +%f`
[postgres@web1 data]$ vim pg_hba.conf
#######################################
# type  database        user            address                 method

# "local" is for unix domain socket connections only
local   all             all                                     trust
# ipv4 local connections:
#host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5     #所有ip都可以通过密码连接
# ipv6 local connections:
host    all             all             ::1/128                 trust
# allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
######################################

  18、前往文件安装所在地,修改postgresql.conf配置文件

[postgres@web1 data]$ cd /usr/local/pgsql/data/
[postgres@web1 data]$ cp postgresql.conf postgresql.conf.`date +%f`
[postgres@web1 data]$ vim postgresql.conf
#########################################
# - connection settings -

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)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/tmp'       # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
#########################################

 

 

  19、创建log目录用于存放开机日志,及启动数据库

[postgres@web1 data]$ mkdir /usr/local/pgsql/log
[postgres@web1 data]$ pg_ctl start -l /usr/local/pgsql-10.5/log/pg_server.log
waiting for server to start.... done
server started
[postgres@web1 data]$ 

  20、检查是否启动,可以看日志

[postgres@web1 data]$ lsof -i:5432
command    pid     user   fd   type device size/off node name
postgres 25479 postgres    3u  ipv4  50990      0t0  tcp *:postgres (listen)
postgres 25479 postgres    4u  ipv6  50991      0t0  tcp *:postgres (listen)
[postgres@web1 data]$ netstat -lnutp|grep postgres
(not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               listen      25479/postgres      
tcp6       0      0 :::5432                 :::*                    listen      25479/postgres      
[postgres@web1 data]$ ps -ef|grep postgres
root     24060  1297  0 18:17 pts/0    00:00:00 su postgres
postgres 24061 24060  0 18:17 pts/0    00:00:00 bash
postgres 25479     1  0 18:45 pts/0    00:00:00 /usr/local/pgsql-10.5/bin/postgres
postgres 25481 25479  0 18:45 ?        00:00:00 postgres: checkpointer process   
postgres 25482 25479  0 18:45 ?        00:00:00 postgres: writer process   
postgres 25483 25479  0 18:45 ?        00:00:00 postgres: wal writer process   
postgres 25484 25479  0 18:45 ?        00:00:00 postgres: autovacuum launcher process  
postgres 25485 25479  0 18:45 ?        00:00:00 postgres: stats collector process  
postgres 25486 25479  0 18:45 ?        00:00:00 postgres: bgworker: logical replication launcher  
postgres 25543 24061  0 18:46 pts/0    00:00:00 ps -ef
postgres 25544 24061  0 18:46 pts/0    00:00:00 grep --color=auto postgres
[postgres@web1 data]$ 

  21、进入库的一些小操作

[postgres@web1 data]$ psql    #进入库
psql (10.5)
type "help" for help.

postgres=# \password    #设置密码
enter new password: 
enter it again: 
postgres=# create database name;       #创建库
create database
postgres=# \du    #查看用户
                                   list of roles
 role name |                         attributes                         | member of 
-----------+------------------------------------------------------------+-----------
 postgres  | superuser, create role, create db, replication, bypass rls | {}

postgres=# 
postgres=# \l    #查看数据库列表
                                  list of databases
   name    |  owner   | encoding |   collate   |    ctype    |   access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 name      | 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)

postgres=# 
postgres=# create user test superuser password '123456';    #创建用户及密码
create role
postgres=# 

*****到此就大致部署完了,如有什么问题以及不足之处,欢迎读客在线批评****