linux(centos 7)安装postgresql

150 阅读3分钟

声明

此安装方式不是走yum安装,源码安装,要会一点linux基础

安装

安装前准备

源码下载:https://www.postgresql.org/ftp/source/

进入root文件夹

[root@localhost ~]# 

安装lrzsz,上传压缩包到linux

[root@localhost ~]# yum -y install lrzsz //安装lrzsz
[root@localhost ~]# rz  //上传,会有个弹框选择后上传

解压安装包

[root@localhost ~]# gzip -d postgresql-14.10.tar.gz
[root@localhost ~]# tar xf postgresql-14.10.tar
[root@localhost ~]# ls

会显示 postgresql-14.10  postgresql-14.10.tar

进入var(我默认安装到这个目录下),建安装目录

[root@localhost ~]# cd /var
[root@localhost var]# mkdir postgresql

进入postgresql-14.10文件夹执行

[root@localhost postgresql-14.10]# ./configure --prefix=/var/postgresql

在我这里会有两个报错,最后是以error开头(我解决了,不能重现,你们注意)
报错:configure: error: readline library not found
解决方法:[root@localhost postgresql-14.10]# yum install -y readline-devel
报错:configure: error: zlib library not found
解决方法:[root@localhost postgresql-14.10]# yum install zlib-devel



最后能看到这些,没报错就成功了

configure: using CPPFLAGS= -D_GNU_SOURCE 
configure: using LDFLAGS=  -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: src/include/pg_config_ext.h is unchanged
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: src/interfaces/ecpg/include/ecpg_config.h is unchanged
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port

编译安装

[root@localhost postgresql-14.10]# make install

看到 Success 表示编译安装成功

fixing permissions on existing directory /var/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: 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:

    /var/postgresql/bin/pg_ctl -D /var/postgresql/data -l logfile start

查看是否安装成功

[root@localhost postgresql-14.10]# cd /var/postgresql
[root@localhost postgresql]# ls

创建数据目录和日志目录

[root@localhost postgresql]# mkdir data log

加入系统环境变量(有两处,还有一处下面创建用户会加) /etc/profile

[root@localhost postgresql]# vi /etc/profile

unset i
unset -f pathmunge

#postgresql 这是备注不要加
export PGHOME=/var/postgresql
export PGDATA=/var/postgresql/data
export PATH=$PATH:$PGHOME/bin

~                                                                                                          
~                                                                                                          
~                                                                                                          
~                                                                                                          
~                                                                                                          
~  

使配置文件生效
[root@localhost postgresql]# source /etc/profile

创建用户组 和 用户,赋予权限

[root@localhost postgresql]# groupadd postgres [root@localhost postgresql]# useradd -g postgres postgres

[root@localhost postgresql]# chown -R postgres:root /var/postgresql

切换用户,进入postgres家目录

[root@localhost postgresql]# su postgres
[postgres@localhost bin]$ cd

加入环境变量 .bash_profile

[postgres@localhost ~]$ vi .bash_profile

# User specific environment and startup programs
export PGHOME=/var/postgresql
export PGDATA=/var/postgresql/data
PATH=$PATH:$PGHOME/bin

export PATH
~                                                                                                          
~                                                                                                          
~ 

使配置文件生效
[postgres@localhost ~]$ source .bash_profile

初始化数据库

[postgres@localhost ~]$ cd /var/postgresql
[postgres@localhost postgresql]$ /var/postgresql/bin/initdb -D /var/postgresql/data

看到 Success 表示初始化数据库

running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: 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:

    /var/postgresql/bin/pg_ctl -D /var/postgresql/data -l logfile start

更改配置使得外网能连接

[postgres@localhost postgresql]$ cd data
[postgres@localhost data]$ vi postgresql.conf

listen_addresses = 'localhost' -> listen_addresses = '*'
port = 5432 -> port = 1921 // 可改可不改

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

启动数据库

[postgres@localhost data]$ cd ..
[postgres@localhost postgresql]$ cd bin
[postgres@localhost bin]$ pg_ctl start -l data

会看到 ,启动成功

waiting for server to start....
 done
server started

查看posogresql进程,是否有进程

[postgres@localhost bin]$ ps -ef | grep postgres

登录数据库

[postgres@localhost bin]$ cd
[postgres@localhost ~]$ su root //切换用户
[root@localhost postgresql]# psql -h127.0.0.1 -Upostgres -p1921

会看到

psql (14.10)
Type "help" for help.

postgres=#

设置postgres密码,用于外网登录

postgres=# ALTER USER postgres with encrypted password 'QAZ123456..'//注意;一定要有

postgres=# exit; //退出(或者不退出新打开ssh窗口)

打开数据库端口(我是1921,你们打开上面设置的端口)

[root@localhost postgresql]# firewall-cmd --zone=public --permanent --add-port=1921/tcp
[root@localhost postgresql]# systemctl restart firewalld //配置生效

好了可以愉快的在外网访问postgresql

把postgresql加入开机自启动

打开配置文件rc.local,修改

[root@localhost ~]# vi /etc/rc.d/rc.local


# that this script will be executed during boot.

在文档的末尾加

touch /var/lock/subsys/local
su postgres -lc "/var/postgresql/bin/pg_ctl start -D /var/postgresql/data"  加这段话
~                                                                                                          
~                                                                                                          
~                                                                                                          
~                                                                                                          
~       

注意在上面启动用的是 start -l ,这里要用 start -D ,start -l我试过没有用

给予权限

chmod 775 /etc/rc.d/rc.local

好了,重启linux

[root@localhost ~]# ps -ef|grep postgres

postgresql 在运行中啦