postgresql-10.4-数据库安装
指尖二进制 • 1 年前 • 753 次点击 • POSTGRESQL
[TOC]
下载地址:https://www.postgresql.org/ftp/source/
编译安装
设置主机名
hostnamectl set-hostname pg01
PostgreSQL编译安装
1:安装必要工具包 readline-devel、zlib-devel、gcc 支持
[root@pg01 ~]# yum install readline-devel zlib-devel gcc
2:解压
[root@pg01 ~]# mkdir -p /pg
[root@pg01 ~]# cd /pg/
[root@pg01 pg]# wget https://ftp.postgresql.org/pub/source/v10.4/postgresql-10.4.tar.gz
[root@pg01 pg]# tar fx postgresql-10.4.tar.gz -C /pg/
3:创建用户与组:
[root@pg01 pg]# groupadd postgres
[root@pg01 pg]# useradd postgres -g postgres
[root@pg01 pg]# echo "postgres" | passwd --stdin postgres
4:创建用来保存初始化数据的目录
[root@pg01 pg]# mkdir /pg/pg_data
5:配置安装路径:
[root@pg01 pg]# cd /pg/postgresql-10.4/
[root@pg01 pg]# ./configure --prefix=/pg/pgsql
6:make 编译,make install 安装
[root@pg01 postgresql-10.4]# make -j 4
[root@pg01 postgresql-10.4]# make install
如果报错checking for DocBook XML V4.2需要安装
yum install docbook-dtds docbook-style-xsl fop libxslt readline-devel.x86_64 zlib-devel.x86_64 -y
7:添加环境变量
[root@pg01 postgresql-10.4]# vim /etc/profile
export PG_HOME=/pg/pgsql
export PGDATA=/pg/pg_data
export PATH=$PATH:$PG_HOME/bin
[root@pg01 postgresql-10.4]# source /etc/profile
[root@pg01 postgresql-10.4]# chown -R postgres.postgres /pg/
8:初始化数据库
[root@pg01 postgresql-10.4]# su - postgres
[postgres@pg01 ~]$ /pg/pgsql/bin/initdb --encoding=UTF8 --lc-collate=en_US.utf8 --lc-ctype=en_US.utf8 -D /pg/pg_data/
9:启动数据库并测试登录与停止数据库
[postgres@pg01 ~]$ pg_ctl start
[postgres@pg01 ~]$ netstat -ntl|grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
tcp6 0 0 ::1:5432 :::* LISTEN
[postgres@pg01 ~]$ psql -Upostgres -d postgres
psql (10.4)
Type "help" for help.
postgres=# \q
[postgres@pg01 ~]$ pg_ctl stop
/pg/pgsql/bin/pg_ctl start -D /pg/pg_data/
/pg/pgsql/bin/pg_ctl stop -D /pg/pg_data
pg数据库配置文件位置
/pg/pg_data/pg_hba.conf
配置开机启动
方法一:
[root@localhost ~]# cat /etc/rc.d/rc.local
su postgres -lc "/pg/pgsql/bin/pg_ctl start -D /pg/pg_data"
方法二:
文件内容如下:PGDATA、PORT和pg_ctl命令的路径根据实际情况填写
vim /usr/lib/systemd/system/postgres.service
[Unit]
Description=PostgreSQLV15 database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=/pg/pg_data
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
#ExecStartPre=/usr/local/pgsql9.4/bin/postgresql-check-db-dir ${PGDATA}
ExecStart=/pg/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/pg/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/pg/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
给文件添加执行权限:添加完权限之后就可以直接的 systemctl 管理了
chmod +x /usr/lib/systemd/system/postgres.service
pg导入sql以及配置远程连接
[root@localhost ~]# su - postgres -c 'pg_ctl start'
zone=# CREATE DATABASE zone;
zone=# CREATE USER root WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'postgres';
zone=# CREATE DATABASE root; #创建root数据库用于root用户psql连接
[root@localhost ~]# psql -d zone -U postgres -f zone.20240516_1.sql
配置远程连接允许指定ip连接数据库
[root@localhost ~]# vim /pg/pg_data/postgresql.conf
listen_addresses = '*'
[root@localhost ~]# vim /pg/pg_data/pg_hba.conf
host all all 10.0.0.1/32 trust
连接名:10.0.0.30-zone
主机名或ip地址:10.0.0.30
端口:5432
初始数据库:zone
用户名:root
密码:postgres