一、官网下载Postgresql安装包
二、上传并解压安装包
创建文件夹,并上传安装包
[root@dragon ~]# mkdir -p /opt/postgres/
解压
[root@dragon postgres]# tar xf postgresql-9.4.12.tar.gz
三、安装依赖包
[root@dragon ~]# yum -y install gcc gcc-c++ readline-devel zlib-devel
四、编译
[root@dragon postgresql-9.4.12]# pwd
/opt/postgres/postgresql-9.4.12
[root@dragon postgresql-9.4.12]# ./configure --prefix=/usr/local/pgsql
### --prefix=/usr/local/pgsql 指定安装目录
五、安装
[root@dragon postgresql-9.4.12]# make && make install
六、创建postgres用户
下面初始化数据库需要使用postgres用户进行
[root@dragon ~]# useradd postgres
创建密码:
[root@dragon ~]# echo 'postgres' | passwd --stdin postgres
给安装目录授权:/usr/local/pgsql/
chown -R postgres:postgres /usr/local/pgsql/
七、初始化数据库
指定工作目录为/usr/local/pgsql/data/
[root@dragon ~]# /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
creating directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... PRC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
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:
./pg_ctl -D /usr/local/pgsql/data -l logfile start
初始化成功之后,会在/usr/local/pgsql/data目录下生成文件
[postgres@dragon ~]$ ll /usr/local/pgsql/data/
总用量 60
drwx------ 5 postgres postgres 41 10月 10 10:38 base
drwx------ 2 postgres postgres 4096 10月 10 10:53 global
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_commit_ts
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_dynshmem
-rw------- 1 postgres postgres 4645 10月 10 10:40 pg_hba.conf
-rw------- 1 postgres postgres 1636 10月 10 10:38 pg_ident.conf
drwx------ 4 postgres postgres 68 10月 10 10:58 pg_logical
drwx------ 4 postgres postgres 36 10月 10 10:38 pg_multixact
drwx------ 2 postgres postgres 18 10月 10 10:53 pg_notify
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_replslot
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_serial
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_snapshots
drwx------ 2 postgres postgres 6 10月 10 10:53 pg_stat
drwx------ 2 postgres postgres 63 10月 10 11:14 pg_stat_tmp
drwx------ 2 postgres postgres 18 10月 10 10:38 pg_subtrans
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_tblspc
drwx------ 2 postgres postgres 6 10月 10 10:38 pg_twophase
-rw------- 1 postgres postgres 3 10月 10 10:38 PG_VERSION
drwx------ 3 postgres postgres 60 10月 10 10:38 pg_wal
drwx------ 2 postgres postgres 18 10月 10 10:38 pg_xact
-rw------- 1 postgres postgres 88 10月 10 10:38 postgresql.auto.conf
-rw------- 1 postgres postgres 22958 10月 10 10:47 postgresql.conf
-rw------- 1 postgres postgres 59 10月 10 10:53 postmaster.opts
-rw------- 1 postgres postgres 78 10月 10 10:53 postmaster.pid
-rw-rw-r-- 1 postgres postgres 439 10月 10 10:53 serverlog
八、修改配置文件
[postgres@dragon data]$ pwd
/usr/local/pgsql/data
[postgres@dragon data]$ vim pg_hba.conf
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all 192.168.0.0/16 trust
host all all 0.0.0.0/0 trust
配置访问ip段控制
[postgres@dragon data]$ cat postgresql.conf |grep -v "^#"|grep -v "^$"|grep -v $'^\t'
listen_addresses = '*'# what IP address(es) to listen on;
port = 5432# (change requires restart)
max_connections = 100# (change requires restart)
shared_buffers = 128MB# min 128kB
dynamic_shared_memory_type = posix# the default is the first option
log_timezone = 'PRC'
datestyle = 'iso, ymd'
timezone = 'PRC'
lc_messages = 'zh_CN.UTF-8'# locale for system error message
lc_monetary = 'zh_CN.UTF-8'# locale for monetary formatting
lc_numeric = 'zh_CN.UTF-8'# locale for number formatting
lc_time = 'zh_CN.UTF-8'# locale for time formatting
default_text_search_config = 'pg_catalog.english'
九、启动数据库
[postgres@dragon ~]$ usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start
waiting for server to start.... done
server started
查看状态:
[postgres@dragon ~]$ systemctl status postgresql
● postgresql.service - SYSV: PostgreSQL RDBMS
Loaded: loaded (/etc/rc.d/init.d/postgresql; bad; vendor preset: disabled)
Active: active (exited) since 一 2022-10-10 10:53:31 CST; 27min ago
Docs: man:systemd-sysv-generator(8)
Process: 941 ExecStart=/etc/rc.d/init.d/postgresql start (code=exited, status=0/SUCCESS)
十、Navicat连接测试