温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PostgreSQL多种方式安装

发布时间:2020-06-15 07:44:28 来源:网络 阅读:685 作者:bigstone2012 栏目:数据库
PostgreSQL

测试环境

测试环境为CentOS7.x x86_64位系统。

前提:

  1. 安装最小化安装

  2. 安装epel源

获取源码

到官方网站获取源代码:

wget https://ftp.postgresql.org/pub/source/v9.4.6/postgresql-9.4.6.tar.bz2


编译安装

对于性能型的软件,我们采用编译的方式进行安装。

安装依赖

yum install -y systemtap-sdt-devel perl-ExtUtils-Embed \
pam-devel libxml2-devel libxslt-devel python-devel

编译

./configure --prefix=/opt/pgsql-9.4.6 \
--with-perl \
--with-python \
--with-openssl \
--with-pam \
--without-ldap \
--with-libxml \
--with-libxslt \
--enable-thread-safety \
--with-wal-blocksize=16 \
--with-blocksize=16 \
--enable-dtrace \
--enable-debug
gmake world # 安装了包含文档,所有的contrib
gmake check-world -- (需要普通用户执行。可选,耗时较长)
gmake install-world

启动服务

软件安装完毕,在操作系统中新建一个普通用户,用于初始化数据库、开启和关闭数据库。

useradd postgres
su - postgres
vi ~/.bash_profile
# add
export PGDATA=/pgdata/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/pgsql-9.4.6
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
export PGUSER=postgres

创建相应的目录并修改权限:

mkdir -pv /pgdata/pg_root
chown -R postgres:postgres /pgdata/pg_root
su - postgres
# 初始化数据
# initdb -D $PGDATA -E UTF8 --locale=C -U postgres -W
# 会提示输入两次密码

在启动数据库之前,需要初始化数据库,在初始化的过程中,会创建配置文件等

修改配置文件

在启动之前,需要修改下pg_hba.conf及postgresql.conf文件,
+ pg_hba.conf用于配置控制访问数据库的来源
+ postgresql.conf是数据库的主要配置文件

最好调整一下内核参数:

vi /etc/sysctl.conf

kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 7672460
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576 

sysctl -p

修改limits.conf配置文件:

vi /etc/security/limits.conf
* soft nofile 131072
* hard nofile 131072
* soft nproc 131072
* hard nproc 131072
* soft core unlimited
* hard core unlimited
* soft memlock 50000000
* hard memlock 50000000

启动数据库

# pg_ctl start -D $PGDATA
# 或者使用如下的方式启动
pg_ctl -D /var/lib/pgsql/data -l logfile start

-bash-4.2$ lsof logfile
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 30772 postgres 1w REG 8,3 0 34606128 logfile
postgres 30772 postgres 2w REG 8,3 0 34606128 logfile
-bash-4.2$ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 30771 postgres 3u IPv6 37671946 0t0 TCP localhost:postgres (LISTEN)
postgres 30771 postgres 4u IPv4 37671947 0t0 TCP localhost:postgres (LISTEN)

允许外网访问:

echo "host all all 0.0.0.0/0 md5" >> $PGDATA/pg_hba.conf

停止

pg_ctl stop -m fast|smart|immediate -D $PGDATA

CentOS二进制包安装

如果认为CentOS或RedHat自带的PostgreSQL版本太低,想要使用新的版本,可以使用下面的方法安装。安装PostgreSQL官方提供的RPM包,将新版本信息加入到版本库中:

rpm -ivh https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-3.noarch.rpm

然后使用yum install命令进行安装:

yum install -y postgresql94-server.x86_64

安装第三方贡献包:

yum install -y postgresql94-contrib.x86_64

新版本的PostgreSQL的数据目录在/var/lib/pgsql/<version>/data目录下,version表示PostgreSQL的版本,如9.4版本就安装在/var/lib/pgsql/9.4/data目录下。

MacOS安装PostgreSQL

可以下载安装Postgres.app即可,这样比较方便学习。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI