目录
1. 安装 初始化数据库 2. PostgrepSQL的简单配置 2.1 修改监听的ip和端口 2.2 修改数据库log相关的参数 2.3 内存参数 3. 数据库的基础操作 3.1 连接数据库控制台 3.2 一些常用控制台命令 3.3 SQL控制台操作语句 4. 认证登录 4.1 认证方式 4.2 远程登录 4.3 本地登录
1. 安装
根据业务需求选择版本, 官网下载
1 2 3 |
yum install https://download. postgresql .org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm yum install postgresql96 postgresql96-server rpm -qa|grep postgre |
初始化数据库
执行完初始化任务之后,postgresql 会自动创建和生成 两个用户 和 一个数据库 :
linux 系统用户 postgres:管理数据库的系统用户; 密码由于是默认生成的,需要在系统中修改一下, $passwd postgres 数据库用户 postgres:数据库超级管理员此 用户 默认数据库 为postgres
1 |
/usr/pgsql-9.6/bin/postgresql96-setup initdb |
设置成 centos7 开机启动服务
1 |
systemctl enable postgresql-9.6 |
启动 postgresql 服务
1 2 |
systemctl start postgresql-9.6 systemctl status postgresql-9.6 |
2. PostgrepSQL的简单 配置
pgsql9.6配置文件位置默认在:/var/lib/pgsql/9.6/data/postgresql.conf
2.1 修改监听的ip和端口
监听IP使用localhost时,只能通过127.0.0.1访问数据库;
如果需要通过其他远程地址访问PostgreSQL,可以使用[,]作为分隔符,把IP地址添加到listen_addresses后,或者使用[*],让所有IP都可以访问数据库。
注意:这里只是开启数据库的 远程访问 权限,具体是否能够进行 远程登录 ,还需要依据 pg_hba.conf 的认证配置,详细内容见下节。
1 2 3 4 5 6 7 |
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) #port = 5432 # (change requires restart) |
2.2 修改数据库log相关的参数
日志收集,一般是打开的
1 2 3 4 5 |
# This is used when logging to stderr: logging_collector = on # Enable capturing of stderr and csvlog # into log files. Required to be on for # csvlogs. # (change requires restart) |
日志目录,一般使用默认值
1 2 3 |
# These are only used if logging_collector is on: log_directory = 'pg_log' # directory where log files are written, # can be absolute or relative to PGDATA |
只保留一天的日志,进行循环覆盖
1 2 3 4 5 6 7 8 9 10 11 12 13 |
log_filename = 'postgresql-%a.log' # log file name pattern, # can include strftime() escapes log_truncate_on_rotation = on # If on, an existing log file of the # same name as the new log file will be # truncated rather than appended to. # But such truncation only occurs on # time-driven rotation, not on restarts # or size-driven rotation. Default is # off, meaning append to existing files # in all cases. log_rotation_age = 1d # Automatic rotation of logfiles will # happen after that time. 0 disables. log_rotation_size = 0 # Automatic rotation of logfiles will |
2.3 内存参数
共享内存的大小,用于共享数据块。如果你的机器上有足够的内存,可以把这个参数改的大一些,这样数据库就可以缓存更多的数据块,当读取数据时,就可以从共享内存中读,而不需要再从文件上去读取。
1 2 3 |
# - Memory - shared_buffers = 32MB # min 128kB # (change requires restart) |
单个SQL执行时,排序、hash json所用的内存,SQL运行完后,内存就释放了。
1 2 |
# actively intend to use prepared transactions. #work_mem = 1MB # min 64kB |
PostgreSQL安装完成后,可以主要修改以下两个主要内存参数:
shared_buffer:共享内存的大小,主要用于共享数据块,默认是128MB;
如果服务器内存有富余,可以把这个参数适当改大一些,这样数据库就可以缓存更多的数据块,当读取数据时,就可以从共享内存中读取,而不需要去文件读取。
work_mem:单个SQL执行时,排序、hash join所使用的内存,SQL运行完成后,内存就释放了,默认是4MB;
增加这个参数,可以提高排序操作的速度。
3. 数据库的基础操作
3.1 连接数据库控制台
如果想连接到数据库,需要切换到postgres用户下(默认的认证配置前提下)
在postgres用户下连接数据库,是不需要密码的。
切换 postgres 用户后,提示符变成 -bash-4.2$
使用psql连接到数据库控制台,此时系统提示符变为'postgres=#'
1 2 3 4 5 6 |
$ su postgres bash-4.2$ psql psql (9.6) Type "help" for help.
postgres=# |
3.2 一些常用控制台命令
\h | 查看所有sql命令,\h select 等可以查看具体命令 |
? | 查看所有psql命令 |
\d | 查看当前数据库所有表 |
\d | [tablename] 查看具体的表结构 |
\du | 查看所有用户 |
\l | 查看所有数据库 |
\e | 打开文本编辑器 |
3.3 SQL控制台操作语句
数据库创建与修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 创建数据库 create database testdb; # 删除数据库 drop database testdb; # 重命名数据库(该数据库必须没有活动的连接) alter database testdb rename to newname; # 以其他数据库为模板创建数据库(表结构、数据都会复制) create database newdb template testdb;
# 将查询结果写入文件 \o /tmp/test.txt select * from test; # 列状显示 \w # 再一次\o关闭写入,否则是连续写入的 \o # 退出控制台 \q |
数据库用户创建与授权
1 2 3 4 5 6 |
# 建立新的数据库用户 create user zhangsan with password '123456'; # 为新用户建立数据库 create database testdb owner zhangsan; # 把新建的数据库权限赋予新用户 grant all privileges on database testdb to zhangsan; |
4. 认证登录
认证权限配置文件: /var/lib/pgsql/9.6/data/pg_hba.conf
命令行的各个参数解释说明:
-U username 用户名,默认值postgres -d dbname 要连接的数据库名,默认值postgres。如果单指定-U,没指定-d参数,则默认访问与用户名名称相同的数据库。 -h hostname 主机名,默认值localhost -p port 端口号,默认值5432
4.1 认证方式
常见的四种身份验证方式
trust:凡是能连接到服务器的,都是可信任的。只需要提供数据库用户名,可以没有对应的操作系统同名用户; password 和 md5:对于外部访问,需要提供 psql 用户名和密码。对于本地连接,提供 psql 用户名密码之外,还需要有操作系统访问权(用操作系统同名用户验证)。password 和 md5 的区别就是外部访问时传输的密码是否用 md5 加密; ident:对于外部访问,从 ident 服务器获得客户端操作系统用户名,然后把操作系统作为数据库用户名进行登录;对于本地连接,实际上使用了peer; peer:通过客户端操作系统内核来获取当前系统登录的用户名,并作为psql用户名进行登录。
4.2 远程登录
postgresql.conf
1 |
listen_addresses = '*' # what IP address(es) to listen on; |
pg_hba.conf
所有的用户通过任意ip都可以通过md5(密码)的方式登陆PostgreSQL,配置如下:
1 |
host all all 0.0.0.0/0 ident |
验证
1 2 3 4 |
# server:重启生效 systemctl restart postgresql-9.6 # client:命令行远程登录 psql -U zhangsan -d testdb -h 10.122.45.97 -p 5432 |
4.3 本地登录
PostgreSQL登陆默认是peer,不需要验证用户密码即可进入psql相关数据库,但前提是必须切换用户登陆。类似于最开始执行的 su postgres;psql 一样。
1 2 |
[root@sltkp3cbpch data]# psql -U zhangsan -d testdb -p 5432 psql: FATAL: Peer authentication failed for user "zhangsan" |
如果必须按照上述登陆方式登陆的话,有两种修改方式:
增添map映射 修改认证方式a. map映射
map 映射是用来将系统用户映射到对应的postgres数据库用户,用来限制指定的用户使用指定的账号来登陆。
pg_ident.conf
修改pg_ident.conf文件,与pg_hba.conf文件同级目录。其基本格式如下:
1 2 |
# MAPNAME SYSTEM-USERNAME PG-USERNAME map_zhangsan root zhangsan |
MAPNAME指的是映射的名称,比如 map_zhangsan SYSTEM-USERNAME就是系统用户的名称,比如 root PG-USERNAME就是数据库里存在的用户名称,比如 zhangsan
上面定义的 map 意思是:定义了一个叫做 map_zhangsan 的映射,当客户端用户是 root 的时候,允许它用 zhangsan 用户来登陆PostgreSQL。
修改pg_hba.conf文件
在peer的认证方式后面添加: map=map_tom
重启PostgreSQL服务,再次尝试,连接成功。
b. 修改认证方式
需要修改一下 pg_hba.cong 文件,将 local all all peer 修改为 local all all md5 ,如下图所示:
重启PostgreSQL服务,再次尝试,连接成功。
到此这篇关于 postgresql安装 及配置超详细教程的文章就介绍到这了,更多相关postgresql安装及配置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/sunhongleibibi/p/11943393.html
查看更多关于postgresql安装及配置超详细教程的详细内容...