访问https://www.postgresql.org/download/,点击左侧的‘source'进行下载,一般选择bz2的安装包。
这里安装12.11版本的postgre,数据目录路径为/data/server/pgdata,端口为5432.
#安装
yum install -y zlib-devel readline-devel perl-ExtUtils-Embed python python-devel gcc bison flex
#检查哪些软件包没装上(若没结果输出,说明全部安装上了)
rpm -q zlib-devel readline-devel perl-ExtUtils-Embed python python-devel gcc bison flex | grep 'not installed'
cd /usr/local
tar -xvf postgresql-12.11.tar.bz2
cd postgresql-12.11
./configure --prefix=/usr/local/pgsql12.11 --enable-thread-safety --with-perl --with-python
/*
假如报错:
checking how to link an embedded Python application... configure: error: could not find shared library for Python
检查下是否安装了多个python,指定PYTHON=正确位置
若还是不行,则加上--without-python:
./configure --prefix=/usr/local/pgsql12.11 --enable-thread-safety --with-perl --without-python
*/
make
make install
ln -s /usr/local/pgsql12.11 /usr/local/pgsql
#安装contrib下的工具
cd contrib
make
make install
vi /etc/profile
在最后添加:
export PATH=$PATH:/usr/local/pgsql/bin
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH
PGDATA=/data/server/pgdata
#让设置生效
source /etc/profile
mkdir -p /data/server
cd /data/server
mkdir pgdata
#新建普通用户,初始化数据库
useradd postgres
passwd postgres
cd /data/server
chown -R postgres:postgres pgdata
su - postgres
export PGDATA=/data/server/pgdata
initdb
cd /data/server/pgdata
mkdir arch
mkdir log
cd /data/server/pgdata
vi postgresql.conf,新增如下内容:
port=5432
listen_addresses='*'
archive_mode=on
archive_command = 'test ! -f /data/server/pgdata/arch/%f && cp %p /data/server/pgdata/arch/%f'
log_destination='stderr'
logging_collector=on
log_directory='log'
log_filename='postgresql-%a.log'
修改:
max_connections = 1000
默认只有本地可以连数据库,远程连接会报错:
psql: FATAL: no pg_hba.conf entry for host "192.168.1.203", user "postgres", database "postgres", SSL off
如果想让别的服务器能够远程连接,需要修改gp_hba.conf文件:
vi pg_hba.conf
添加如下一行内容,以允许所有服务器连接
host all all 0.0.0.0/0 md5
pg_ctl -D /data/server/pgdata start
psql -p 端口
报错:
pg日志里显示:
DETAIL: User "postgres" has no password assigned
解决办法:进数据库里为该用户设置密码:
ALTER USER postgres PASSWORD '设为和该操作系统用户一样的密码';
如果是CentOS 6,则选择用2.8.1这种方式,如果是CentOS 7,则选择用2.8.2这种方式.
用root用户执行:
cp /usr/local/postgresql-12.11/contrib/start-scripts/linux /etc/init.d/postgresql
chmod +x /etc/init.d/postgresql
chown postgres:postgres /etc/init.d/postgresql
vi /etc/init.d/postgresql,注意修改这两个地方:
prefix=/usr/local/pgsql
PGDATA="/data/server/pgdata"
#先关闭之前启动的数据库
usr/local/pgsql/bin/pg_ctl stop -D /data/server/pgdata
#验证用service方式启停数据库
#启库命令
service postgresql start
#关库命令
service postgresql stop
--备注:
需要用root用户执行service命令启停数据库,该启动脚本里会自动切换到postgres用户
chkconfig --add postgresql
cd /etc/systemd/system
vi postgresql.service,新增如下内容:
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
Documentation=http://www.postgresql.org/docs/12/static/
After=network.target
[Service]
Type=forking
User=postgres
ExecStart=/usr/local/pgsql/bin/pg_ctl start -D /data/server/pgdata
ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D /data/server/pgdata
ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D /data/server/pgdata
TimeoutSec=300
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
#先关闭之前启动的数据库
usr/local/pgsql/bin/pg_ctl stop -D /data/server/pgdata
#验证用systemctl方式启停数据库
#启库命令
systemctl start postgresql
#关库命令
systemctl stop postgresql
systemctl enable postgresql