1、安装环境
环境:
- postgresql-14.2
- CentOS Linux release 7.4.1708 (Core)
- 注意:确认linux系统可以正常连接网络,因为在后面需要添加依赖包。
2、pg数据库安装包下载
下载地址:http://www.postgresql.org/ftp/source/
清华源地址:https://mirrors.tuna.tsinghua.edu.cn/postgresql/source/
选择要安装的版本进行下载:
- postgresql-14.2.tar.gz 下载地址:https://ftp.postgresql.org/pub/source/v14.2/postgresql-14.2.tar.gz
3、安装依赖包
在要安装postgresql数据库的Linux服务器(hostname:weekend02)上执行以下命令安装所需要的依赖包:
yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
4、安装postgres
安装步骤
- 在根目录下新建pgsql文件夹,并将pgsql的压缩包移入。
- 解压压缩包
- 进入解压后的文件夹
- 编译postgresql源码
[root@i-vfyxrtmx soft]
postgresql-14.2.tar.gz
[root@i-vfyxrtmx soft]
[root@i-vfyxrtmx soft]
[root@i-vfyxrtmx soft]
[root@i-vfyxrtmx postgresql-14.2]
aclocal.m4 config configure configure.ac contrib COPYRIGHT doc GNUmakefile.in HISTORY INSTALL Makefile README src
[root@i-vfyxrtmx postgresql-14.2]
[root@i-vfyxrtmx postgresql-14.2]
选项 描述
–prefix=prefix 安装到prefix指向的目录;默认为/usr/local/pgsql
–bindir=dir 安装应用程序到dir;默认为prefix/bin
–with-docdir=dir 安装文档到dir;默认为prefix/doc
–with-pgport=port 设置默认的服务器端网络连接服务TCP端口号
–with-tcl 为服务端提供Tcl存储过程支持
–with-perl 为服务端提供Perl存储过程支持
–with-python 为服务端提供Python存储过程支持
[root@i-vfyxrtmx postgresql-14.2]
......
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/opt/soft/postgresql-14.2/src/test/perl'
make[1]: Leaving directory `/opt/soft/postgresql-14.2/src'
make -C config all
make[1]: Entering directory `/opt/soft/postgresql-14.2/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/opt/soft/postgresql-14.2/config'
[root@i-vfyxrtmx postgresql-14.2]# make install
......
make -C config install
make[1]: Entering directory `/opt/soft/postgresql-14.2/config'
/usr/bin/mkdir -p '/opt/pgsql/postgresql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/pgsql/postgresql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/pgsql/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/opt/soft/postgresql-14.2/config
[root@i-vfyxrtmx postgresql]
[root@i-vfyxrtmx postgresql]
/opt/pgsql/postgresql
[root@i-vfyxrtmx postgresql]
bin include lib share
5、创建用户 、用户组
[root@weekend02 postgresql-11.1]
[root@weekend02 postgresql-11.1]
[root@weekend02 postgresql-11.1]
uid=501(postgres) gid=501(postgres) 组=501(postgres)
6、创建数据主目录
[root@i-vfyxrtmx postgresql]
[root@i-vfyxrtmx postgresql]
[root@i-vfyxrtmx postgresql]
[root@i-vfyxrtmx postgresql]
total 28
drwxr-xr-x 7 root root 4096 Mar 7 16:25 .
drwxr-xr-x 3 root root 4096 Mar 7 16:16 ..
drwxr-xr-x 2 root root 4096 Mar 7 16:23 bin
drwxr-xr-x 2 postgres postgres 4096 Mar 7 16:25 data
drwxr-xr-x 6 root root 4096 Mar 7 16:23 include
drwxr-xr-x 4 root root 4096 Mar 7 16:23 lib
drwxr-xr-x 6 root root 4096 Mar 7 16:23 share
7、配置环境变量
[root@i-vfyxrtmx postgresql]
[root@i-vfyxrtmx postgres]
[root@i-vfyxrtmx postgres]
total 20
drwx------ 2 postgres postgres 4096 Mar 7 16:24 .
drwxr-xr-x. 3 root root 4096 Mar 7 16:24 ..
-rw-r--r-- 1 postgres postgres 18 Aug 3 2017 .bash_logout
-rw-r--r-- 1 postgres postgres 193 Aug 3 2017 .bash_profile
-rw-r--r-- 1 postgres postgres 231 Aug 3 2017 .bashrc
[root@weekend02 postgres]
export PGHOME=/opt/pgsql/postgresql
export PGDATA=$PGHOME/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin
[root@weekend02 postgres]
8、initdb初使化数据库
[root@weekend02 postgres]
[postgres@i-vfyxrtmx ~]$ initdb
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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /opt/pgsql/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: 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 /opt/pgsql/postgresql/data -l logfile start
[postgres@i-vfyxrtmx ~]$ ls -lh $PGDATA
total 120K
drwx------ 5 postgres postgres 4.0K Mar 7 16:29 base
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 global
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_commit_ts
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_dynshmem
-rw------- 1 postgres postgres 4.7K Mar 7 16:29 pg_hba.conf
-rw------- 1 postgres postgres 1.6K Mar 7 16:29 pg_ident.conf
drwx------ 4 postgres postgres 4.0K Mar 7 16:29 pg_logical
drwx------ 4 postgres postgres 4.0K Mar 7 16:29 pg_multixact
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_notify
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_replslot
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_serial
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_snapshots
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_stat
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_stat_tmp
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_subtrans
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_tblspc
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_twophase
-rw------- 1 postgres postgres 3 Mar 7 16:29 PG_VERSION
drwx------ 3 postgres postgres 4.0K Mar 7 16:29 pg_wal
drwx------ 2 postgres postgres 4.0K Mar 7 16:29 pg_xact
-rw------- 1 postgres postgres 88 Mar 7 16:29 postgresql.auto.conf
-rw------- 1 postgres postgres 29K Mar 7 16:29 postgresql.conf
9、配置服务
[postgres@i-vfyxrtmx data]$ vi postgresql.conf
listen_addresses = '*'
port = 5432
[postgres@weekend02 data]$ vi pg_hba.conf
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust
10、设置开机自启动
[root@i-vfyxrtmx start-scripts]
/opt/soft/postgresql-14.2/contrib/start-scripts
[root@i-vfyxrtmx start-scripts]
[root@i-vfyxrtmx start-scripts]
freebsd linux macos
[root@i-vfyxrtmx start-scripts]
[root@i-vfyxrtmx start-scripts]
prefix设置为postgresql的安装路径(根目录):prefix=/opt/pgsql/postgresql
PGDATA设置为postgresql的数据目录路径:PGDATA="/opt/pgsql/postgresql/data"
普通账户名称:PGUSER=postgres
日志目录:PGLOG="$PGDATA/serverlog"
[root@weekend02 init.d]
[root@i-vfyxrtmx start-scripts]
......
postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
systemctl status firewalld
systemctl start firewalld
firewall-cmd --permanent --zone=public --add-service=postgresql
firewall-cmd --reload
[root@weekend02 sysconfig]
[root@weekend02 sysconfig]
-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT
[root@weekend02 sysconfig]
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@weekend02 sysconfig]
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:5432
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
6)执行service postgresql start,启动PostgreSQL服务
[root@weekend02 init.d]
Starting PostgreSQL: ok
[root@i-vfyxrtmx sysconfig]
root 4654 16032 0 16:29 pts/0 00:00:00 su - postgres
postgres 4655 4654 0 16:29 pts/0 00:00:00 -bash
postgres 11983 1 0 16:50 ? 00:00:00 /opt/pgsql/postgresql/bin/postmaster -D /opt/pgsql/postgresql/data
postgres 11985 11983 0 16:50 ? 00:00:00 postgres: checkpointer
postgres 11986 11983 0 16:50 ? 00:00:00 postgres: background writer
postgres 11987 11983 0 16:50 ? 00:00:00 postgres: walwriter
postgres 11988 11983 0 16:50 ? 00:00:00 postgres: autovacuum launcher
postgres 11989 11983 0 16:50 ? 00:00:00 postgres: stats collector
postgres 11990 11983 0 16:50 ? 00:00:00 postgres: logical replication launcher
postgres 13614 11983 0 16:55 ? 00:00:00 postgres: postgres postgres 10.92.1.2(12814) idle
root 14690 6960 0 16:58 pts/2 00:00:00 grep --color=auto postgres
[root@i-vfyxrtmx sysconfig]
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 11983/postmaster
tcp6 0 0 :::5432 :::* LISTEN 11983/postmaster
11、开始测试
$ su - postgres
$ psql
创建数据库用户
赋予账号权限
新建数据库
退出
[postgres@weekend02 ~]$ psql
psql (11.1)
Type "help" for help.
postgres=
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=
CREATE ROLE
postgres=
ALTER ROLE
postgres=
CREATE DATABASE
postgres=
重新登录数据库
输入密码
显示数据库
[postgres@weekend02 ~]$ psql -U pg -d pg -h i-vfyxrtmx
psql (11.1)
Type "help" for help.
pg=
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
pg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
pg=
说明数据库安装成功。
12、navicat远程测试
|