pg_13安装

1.安装(rpm包安装)

1.1 rpm安装

https://www.postgresql.org/download
https://yum.postgresql.org/repopackages.php
文档:https://www.postgresql.org/download/linux/redhat/
rpm下载:https://yum.postgresql.org/rpmchart/

1.2 依赖包

-- 一些依赖包
yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel perl python36 tcl openssl ncurses-devel openldap pam


-- 删除已存在的PG
yum remove -y postgresql* && rm -rf /var/lib/pgsql && rm -rf /usr/pgsql* && userdel -r postgres && groupdel postgres
yum install -y sysbench

-- 安装yum源
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum repolist all | grep pgdg
psql

-- 修改postgres密码
alter user postgres with encrypted password 'test';\password
select * from pg_tables;
select version();

1.3 防火墙设置

firewall-cmd --add-port=5432/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port

1.4 允许远程登录

cat >> /var/lib/pgsql/data/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/var/lib/pgsql/12/data'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y%m%d.log'
log_truncate_on_rotation = on
EOF

cat << EOF > /var/lib/pgsql/12/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 md5
EOF

systemctl restart postgresql-12


-- 远程登陆
psql -U postgres -h 192.168.66.35 -d postgres -p5432

CREATE DATABASE test WITH OWNER=postgres ENCODING='UTF-8';
\c test

create table student (
id integer not null,
name character(32),
number char(5),
constraint student_pkey primary key (id)
);

\d student

INSERT INTO student (id, name, number) VALUES (1, '张三', '1023'); 
SELECT * FROM student WHERE id=1;

2.编译安装

2.1 下载安装包

https://www.postgresql.org/ftp/source/
wget https://ftp.postgresql.org/pub/source/v13.2/postgresql-13.2.tar.gz

2.2 创建用户及相关路径

groupadd -g 60000 pgsql
useradd -u 60000 -g pgsql pgsql
echo "lhr" | passwd --stdin pgsql
mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg13,soft}
chown -R pgsql:pgsql /postgresql
chmod -R 775 /postgresql

2.3 编译

su - pgsql
cd /postgresql/soft
tar zxvf postgresql-13.2.tar.gz
cd postgresql-13.2
./configure --prefix=/postgresql/pg13 --without-readline
make -j 8 && make install

2.4 配置环境变量

cat >> ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg13
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 DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres

alias psql='rlwrap psql' 
EOF

source ~/.bash_profile

2.5 初始化

su - pgsql
/postgresql/pg13/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres

2.6修改配置参数文件

cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%Y%m%d.log'
log_truncate_on_rotation = on
EOF

cat > /postgresql/pgdata/pg_hba.conf << EOF
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 md5
EOF

2.7 启停pg

启动
su - pgsql
pg_ctl start
pg_ctl status
pg_ctl stop

你可能感兴趣的:(pg)