温锦文欧普厨电及净水器总代理 发表于 前天 13:10

Linux环境中对Postgrel数据库的安装与配置

一、环境准备

linux操作系统的环境是centos7;
Postgrel数据库的版本是12.0,不同版本的下载渠道如下(PostgreSQL: File Browser):
https://i-blog.csdnimg.cn/direct/bffc690f40e54495801c8a5b94fcf21b.pnghttps://i-blog.csdnimg.cn/direct/3beb9945ccb44b4fa9e22e54cb6dbda7.png
可以看到压缩包是比较小的;下载之后,上传到你的linux环境中即可。
二、安装所需要的依赖

2.1准备依赖

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel 注意:这里由于cenos7已经不再继续维护,以是所需要的依赖大概会下载失败,这需要重新配置yum源,换成aliyun的镜像,应该能下载乐成(这是我的重新编辑yum源的过程:centos7在使用yum源安装依赖时报错_centos7安装源堕落-CSDN博客);
https://i-blog.csdnimg.cn/direct/4da56161ca7b470da14301396ef36a7a.png
所需的依赖被下载安装乐成;
2.2接着就是解压安装包

tar -zxvf postgresql-12.0.tar.gz 解压之后的文件夹内容如下:
https://i-blog.csdnimg.cn/direct/43775a2d8d3c4e7089adfd67f0c1303d.png
三、配置安装

安装包解压之后的文件夹内里有configure的可执行文件,执行如下操作(背面的这个路径可以改成自己的风格,也可不改):
3.1主动配置

./configure --prefix=/opt/pgsql/postgresql https://i-blog.csdnimg.cn/direct/ea9f706ed1dc40d6b314797e8b844b8c.png
3.2编译

make https://i-blog.csdnimg.cn/direct/d81af2efaaa246f19fc388fcc68fa820.png
3.3编译安装

make install https://i-blog.csdnimg.cn/direct/ad46413687174ab69fab98754b5b5e5e.png
3.4创建用户

#添加用户组
groupadd postgres

#添加用户
useradd -g postgres postgres https://i-blog.csdnimg.cn/direct/5968bf1c68e74e72954a4f08600527ba.png
然后就是在你的安装目录下面创建一个存放数据文件的文件夹,我这里叫data
mkdir data https://i-blog.csdnimg.cn/direct/897f409408a0475b88ed0cf2abec4a09.png
并给改数据文件夹授权
chown postgres:postgres data https://i-blog.csdnimg.cn/direct/ffc4a7eab3d84f43bf2f0d0594059ecd.png
3.5修改配置文件

接着是切换到home下进入到postgres下:
cd postgres/ https://i-blog.csdnimg.cn/direct/0fba58676d794c73960b71fdcfb52989.png
https://i-blog.csdnimg.cn/direct/33f146e89c4744b287460133f5620177.png
对文件.bash_profile进行编辑:
vim .bash_profile  添加如下配置:
#PostGrel
export PGHOME=/opt/pgsql/postgresql

export PGDATA=/opt/pgsql/postgresql/data

PATH=$PATH:$HOME/bin:$PGHOME/bin

https://i-blog.csdnimg.cn/direct/a2394da208f34a0e9a648c5771fb03e4.png
生存退出;
接着执行如下命令,使其生效即可
source .bash_profile 然后切换到postgres用户下,初始化数据库
su - postgres

initdb https://i-blog.csdnimg.cn/direct/f921c8233a9345a686e70c1f4cfb1caa.png
在初始化之后,我们的data文件夹内里就有了一些必要的文件,紧接着修改两个配置文件:
https://i-blog.csdnimg.cn/direct/ef9a461c98d54e9386d28210c54a0179.png
vim postgresql.conf 这个参数listen_addresses原来是被注释掉的,但是如果想从其他的机器来访问该服务器上的pg数据库,就需要将该参数启用,并将内里的localhost换为*
https://i-blog.csdnimg.cn/direct/8db7bc7a7d134593b687bd5b78d52167.png
生存并退出;
vim pg_hba.conf 新增这一行才能使局域网来访问:
host    all            all             0.0.0.0/0               trust https://i-blog.csdnimg.cn/direct/bf8021770f3e4a5ab0cc868cb2cb037c.png
生存并退出;
3.6配置自启动

在解压的安装包中,进入到如下文件夹中:
https://i-blog.csdnimg.cn/direct/4dcdda04f16c4bdcbe17121702409ca8.png
https://i-blog.csdnimg.cn/direct/244196a55a4e49d48966dd5ed16eddce.png
https://i-blog.csdnimg.cn/direct/5bae74c9476a44e6803adb3079b47594.png
授权:
chmod a+x linux https://i-blog.csdnimg.cn/direct/aa8635424b5949418f8b6cafe23ac1d6.png
然后将这里的linux复制到/etc/init.d中,并改名
cp linux /etc/init.d/postgresql https://i-blog.csdnimg.cn/direct/596f754ab4f948ba9933f0396a4ef7d7.png
编辑该文件:
https://i-blog.csdnimg.cn/direct/0242ff391cb7407fabfe56bd76a6cce2.png
vim /etc/init.d/postgresql 修改这两个参数,换成自己的
https://i-blog.csdnimg.cn/direct/d192d51763e64e4fb34c7a3b42018d49.png
修改后:
https://i-blog.csdnimg.cn/direct/139940a1597342fa8554177cbb3a8701.png
生存退出;
接着执行如下命令:
chkconfig --add postgresql https://i-blog.csdnimg.cn/direct/879cf455cacd4000ba8af82ba43f5398.png
启动pg服务:
service postgresql start https://i-blog.csdnimg.cn/direct/a30d172e7f8e4283ad147cf3f4392073.png
查看服务进程:
https://i-blog.csdnimg.cn/direct/a6abbd2e91bf43ce8e9300c893bac4a6.png
四、简单使用

(1)直接进入到命令行下:
https://i-blog.csdnimg.cn/direct/3664cb5b5d914f5082925f9a0e5d7026.png
(2)也可以通过如下方式进入命令行:
psql -h <hostname or ip> -p <端口> [数据库名称] [用户名称]  4.1常用命令

注意:实在一项技能最好的辅助课本就是它的官方文档;(PostgreSQL 12.2 手册)
PG的官方文档环视不错的,相比较Oracle和MongoDB的官方文档,技能比较全且是比较正确的中文翻译;
注意:psql 的命令都是以斜杠 "\" 开头的。
  (1)   \l  查看全部数据库
(2)\d [ pattern ] 该命令将表现每个匹配关系(表、视图、索引、序列)的信息,可以加上 + 查看更多的信息,如 \d+
不加任何参数表示查看当前数据库的全部表。
 \d tablename 背面跟一个表名,表示表现这个表的布局定义
\d indexname 也可以表现索引的信息,如 \d 索引名称
\d * 背面也可以跟通配符如 "*" 或 "?",\d x*
\d+ 将表现比 \d 更具体的信息,还会表现任何与表关系的注释,以及表中出现的 OID 
\dt 只表现匹配的表  
\di 只表现索引
 \ds  只表现序列
\dv 只表现视图
\df 只表现函数
(3) \timing on或off 表现 SQL 已执行的时间,默认环境下是 off
(4)\dn 列出全部的 schema 
(5)\du 或 \dg 列出全部的数据库用户和角色
(6)\db 表现全部的表空间,表空间实在是一个目录,放在这个表空间的表,就是把表的数据文件发到这个表空间下。
(7)\dp 或 \z 表现表的权限分配环境
(8)\encoding 指定客户端的字符编码,如 \encoding UTF8; 
(9)\pset 设置输出的格式,\pset border 0 : 表示输出内容无边框。 border 1 :表示边框只在内部。border 2 :内外都有边框
(10)\x 把表中的每一行的每列数据都拆分为单行展示,与 MySQL 中的 "\G" 的功能雷同。
(11)\echo 用于输出一行信息,通常用于在 .sql 文件中输出一些提示信息。 
(12)\password 设置密码
(13)\conninfo 列出当前数据库连接的信息
(14)\dx 查看数据库中安装的扩展 或 select * from pg_extension;
   全部的\用法:
General
  \copyright             show PostgreSQL usage and distribution terms
  \crosstabview execute query and display results in crosstab
  \errverbose            show most recent error message at maximum verbosity
  \g or ;         execute query (and send results to file or |pipe)
  \gdesc                 describe result of query, without executing it
  \gexec                 execute query, then execute each value in its result
  \gset         execute query and store results in psql variables
  \gx             as \g, but forces expanded output mode
  \q                     quit psql
  \watch           execute query every SEC seconds
Help
  \?          show help on backslash commands
  \? options             show help on psql command-line options
  \? variables           show help on special variables
  \h              help on syntax of SQL commands, * for all commands
Query Buffer
  \e       edit the query buffer (or file) with external editor
  \ef ]  edit function definition with external editor
  \ev ]  edit view definition with external editor
  \p                     show the contents of the query buffer
  \r                     reset (clear) the query buffer
  \s              display history or save it to file
  \w FILE                write query buffer to file
Input/Output
  \copy ...              perform SQL COPY with data stream to the client host
  \echo         write string to standard output
  \i FILE                execute commands from file
  \ir FILE               as \i, but relative to location of current script
  \o              send all query results to file or |pipe
  \qecho        write string to query output stream (see \o)
Conditional
  \if EXPR               begin conditional block
  \elif EXPR             alternative within current conditional block
  \else                  final alternative within current conditional block
  \endif                 end conditional block
Informational
  (options: S = show system objects, + = additional detail)
  \d                 list tables, views, and sequences
  \d  NAME           describe table, view, sequence, or index
  \da        list aggregates
  \dA[+]        list access methods
  \db[+]        list tablespaces
  \dc      list conversions
  \dC[+]        list casts
  \dd        show object descriptions not displayed elsewhere
  \dD      list domains
  \ddp          list default privileges
  \dE      list foreign tables
  \det[+]      list foreign tables
  \des[+]      list foreign servers
  \deu[+]      list user mappings
  \dew[+]      list foreign-data wrappers
  \df list functio
ns
  \dF[+]        list text search configurations
  \dFd[+]      list text search dictionaries
  \dFp[+]      list text search parsers
  \dFt[+]      list text search templates
  \dg      list roles
  \di      list indexes
  \dl                    list large objects, same as \lo_list
  \dL      list procedural languages
  \dm      list materialized views
  \dn      list schemas
  \do        list operators
  \dO      list collations
  \dp          list table, view, and sequence access privileges
  \dP    list partitioned relations
  \drds ] list per-database role settings
  \dRp[+]      list replication publications
  \dRs[+]      list replication subscriptions
  \ds      list sequences
  \dt      list tables
  \dT      list data types
  \du      list roles
  \dv      list views
  \dx[+]        list extensions
  \dy          list event triggers
  \l[+]        list databases
  \sf[+]  FUNCNAME       show a function's definition
  \sv[+]  VIEWNAME       show a view's definition
  \z            same as \dp
Formatting
  \a                     toggle between unaligned and aligned output mode
  \C            set table title, or unset if none
  \f            show or set field separator for unaligned query output
  \H                     toggle HTML output mode (currently off)
  \pset ]   set table output option
                         (border|columns|csv_fieldsep|expanded|fieldsep|
                         fieldsep_zero|footer|format|linestyle|null|
                         numericlocale|pager|pager_min_lines|recordsep|
                         recordsep_zero|tableattr|title|tuples_only|
                         unicode_border_linestyle|unicode_column_linestyle|
                         unicode_header_linestyle)
  \t            show only rows (currently off)
  \T            set HTML <table> tag attributes, or unset if none
  \x       toggle expanded output (currently off)
Connection
  \c { | conninfo}
                         connect to new database (currently "postgres")
  \conninfo              display information about current connection
  \encoding   show or set client encoding
  \password   securely change the password for a user
Operating System
  \cd              change the current working directory
  \setenv NAME   set or unset environment variable
  \timing       toggle timing of commands (currently off)
  \!           execute command in shell or start interactive shell
Variables
  \prompt NAME    prompt user to set internal variable
  \set ]    set internal variable, or list all if no parameters
  \unset NAME            unset (delete) internal variable
Large Objects
  \lo_export LOBOID FILE
  \lo_import FILE
  \lo_list
  \lo_unlink LOBOID      large object operations
4.2执行存储在外部文件中的 SQL 命令

(1)\i <文件名> 执行存储在外部文件中的 sql 语句
(2)当然也可以在 psql 命令行加 "-s <filename>" 来执行 SQL 脚本文件中的命令,如 psql -s test.sql
4.3psql 的使用技巧和注意事项

(1)在启动 psql 命令后中加 "-E" 参数,就可以把 psql 中各种以 "\" 开头的命令执行的实际 SQL 打印出来,如下 psql -E postgres,如果想关闭此功能,可以使用 "\set ECHO_HIDDEN on | off"
(2)主动提交方面的技巧
psql 中的事务是主动提交的,可以运行 begin; 然后执行 dml 语句,末了再执行 commit 或 rollback 语句。或 直接使用 psql 中的命令关闭主动提交的功能。 \set AUTOCOMMIT off
(3)查看数据库、表、索引大小
select pg_size_pretty(pg_table_size('test'));   
select pg_size_pretty(pg_database_size('david'));
select pg_size_pretty(pg_indexes_size('test')); <strong>完结。</strong>

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux环境中对Postgrel数据库的安装与配置