一:安装环境
1.PostgresSQL版本:postgresql-10.1
1.1资源下载地址 https://www.postgresql.org/ftp/source/v10.1/ ----》下载 postgresql-10.1.tar.gz
2.linux: CentOS release 7 --我放在了虚拟机里,
二:安装
第一步:
1.创建安装路径 /usr/local/pgsql
1.1创建路径
mkdir -p /usr/local/pgsql
1.2复制 postgresql-10.1.tar.gz 到 /usr/local/pgsql下
2.解压安装文件
gunzip postgresql-10.1.tar.gz
tar xf postgresql-10.1.tar
3.执行安装命令
./configure
make
su
make install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data --目录权限给到postgres用户
chown postgres /usr/local/pgsql/bin
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
【./configure是检查当前环境能否安装PG,以下是我遇到的报错和解决方案
报错1:configure: error: no acceptable C compiler found in $PATH:
解决:yum install gcc
报错2:configure: error: readline library not found
解决:yum install readline-devel
报错3: error: zlib library not found解决方法
解决方法
yum install zlib-devel 】
4. 启动或重启server
4.1先切换用户postgres,然后切到/pgsql/bin目录下
su - postgres
cd /usr/local/pgsql/bin/
启动server:
./pg_ctl start -D /usr/local/pgsql/data
结果如下
PostgreSQL安装成功。
重启server 命令:
./pg_ctl restart -D /usr/local/pgsql/data
参考备注【
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.
Usage:
pg_ctl init[db] [-D DATADIR] [-s] [-o OPTIONS]
pg_ctl start [-D DATADIR] [-l FILENAME] [-W] [-t SECS] [-s]
[-o OPTIONS] [-p PATH] [-c]
pg_ctl stop [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
pg_ctl restart [-D DATADIR] [-m SHUTDOWN-MODE] [-W] [-t SECS] [-s]
[-o OPTIONS] [-c]
pg_ctl reload [-D DATADIR] [-s]
pg_ctl status [-D DATADIR]
pg_ctl promote [-D DATADIR] [-W] [-t SECS] [-s]
pg_ctl kill SIGNALNAME PID
Common options:
-D, --pgdata=DATADIR location of the database storage area
-s, --silent only print errors, no informational messages
-t, --timeout=SECS seconds to wait when using -w option
-V, --version output version information, then exit
-w, --wait wait until operation completes (default)
-W, --no-wait do not wait until operation completes
-?, --help show this help, then exit
If the -D option is omitted, the environment variable PGDATA is used.
Options for start or restart:
-c, --core-files allow postgres to produce core files
-l, --log=FILENAME write (or append) server log to FILENAME
-o, --options=OPTIONS command line options to pass to postgres
(PostgreSQL server executable) or initdb
-p PATH-TO-POSTGRES normally not necessary
Options for stop or restart:
-m, --mode=MODE MODE can be "smart", "fast", or "immediate"
Shutdown modes are:
smart quit after all clients have disconnected
fast quit directly, with proper shutdown (default)
immediate quit without complete shutdown; will lead to recovery on restart
Allowed signal names for kill:
ABRT HUP INT QUIT TERM USR1 USR2
Report bugs to <pgsql-bugs@postgresql.org>.
翻译如下:
pg_ctl是初始化、启动、停止或控制PostgreSQL服务器的实用程序。
用法:
pg_ctl初始化[数据库][-D数据目录][-s[-o选项]
pg_ctl开始[-D数据目录][-l文件名][-W[-t秒][-s]
[-o选项][-p路径][-c]
pg_ctl stop[-D数据目录][-m关闭模式][-W[-t秒][-s]
pg_ctl重新启动[-D数据目录][-m关闭模式][-W[-t秒][-s]
[-o选项][-c]
pg_ctl重新加载[-D数据目录][-s]
pg_ctl status[-D数据目录]
pg_ctl提升[-D数据目录][-W[-t秒][-s]
pg_ctl kill SIGNALNAME PID
常用选项:
-D、 --pgdata=DATADIR数据库存储区的位置
-s、 --仅静默打印错误,无信息性消息
-t、 --timeout=SECS使用-w选项时等待的秒数
-V,-版本输出版本信息,然后退出
-w、 --等待操作完成(默认)
-W、 --不等待不要等到操作完成
-?-帮助显示此帮助,然后退出
如果省略-D选项,则使用环境变量PGDATA。
启动或重新启动选项:
-c、 --核心文件允许postgres生成核心文件
-l、 --log=FILENAME将服务器日志写入(或追加)到FILENAME
-o、 --options=options要传递给postgres的命令行选项
(PostgreSQL server可执行文件)或initdb
-p路径到POSTGRES通常不需要
停止或重新启动选项:
-m、 --mode=模式模式可以是“smart”、“fast”或“immediate”
关机模式为:
所有客户端断开连接后智能退出
直接快速退出,适当关机(默认)
立即退出而不完全关闭;将导致在重新启动时恢复
允许的杀死信号名称:
ABRT HUP INT退出学期USR1 USR2
向<pgsql bugs@postgresql.org>报告错误。】
5、 新建数据库和可以登录数据库的用户密码
5.1创建数据库:
在/usr/local/pgsql/bin/目录下
./createdb testdb
5.2创建用户
第一种方法:create user
进入默认的postgres数据库:
./psql
然后执行:
CREATE USER user1 WITH PASSWORD '123456';
备注:CREATE USER OR CREATE ROLE:
CREATE USER是CREATE ROLE的一个别名。
区别:CREATE USER命令缺省是LOGIN, CREATE ROLE命令缺省是NOLOGIN。
第二种方法:pg封装的命令
./createuser -P lin
会提示输入新建用户的密码,重复输入密码后,创建成功
6、访问数据库 /usr/local/pgsql/bin/目录下 以默认用户名访问默认数据库(默认的用户名和数据库名都是postgres):
./psql
进入PG的交互终端psql
以名为lin的角色登录名为testdb的数据库:
./psql testdb -U user1
当psql终端的提示符为=#时,表示当前登录的是超级用户,而当提示符为=>时则为普通用户
7、远程访问数据库设置
* 远程访问数据库的认证方式主要有很多方式,我只设置基于TCP/IP连接的trust认证方式
需设置两个配置文件,
1)修改配置文件postgresql.conf,
vim /usr/local/pgsql/data/postgresql.conf
修改监听地址:
#listen_addresses=’localhost’
#将上面这行改成如下
listen_addresses=’*’
2)修改配置文件/pgsql/data/pg_hba.conf:
vim /usr/local/pgsql/data/pg_hba.conf
添加一条IP授权记录(如192.168.8.200),可以对一个网段授权
# IPv4 myhost connections:
host all all 192.168.88.0/210 trust
设置所有网段IP可以访问:
# IPv4 remote address connections:
host all all 0.0.0.0/0 trust
设置完,重启数据库生效
我用的是 dbvis 工具
8.1第一种连接方法
如上图 1是posgre 库的信息,2是虚拟机的信息,我的linux是放在虚拟机里,所以有2的配置。你也可以不选2.
测试连接,点击 connect 测试结果如下:
8.2第二种连接方法
8.2.1使用 root 用户, 查看Linux系统上开放的端口号
[root@localhost /]#firewall-cmd --zone=public --list-ports
8.2.2使用 root 用户, 输入设置5432端口开放
[root@localhost /]#firewall-cmd --zone=public --add-port=5432/tcp --permanent
8.2.3查询8080端口是否设置成功
[root@localhost /]#firewall-cmd --zone=public --query-port=5432/tcp
结果:no
8.2.4还需要重启一下防火墙
[root@localhost /]#systemctl restart firewalld.service
8.2.5再次查询5432端口是否设置成功
[root@localhost /]#firewall-cmd --zone=public --query-port=8080/tcp
设置成功
8.2.6windows图形工具重新连接
连接成功