PG学习初体验--源码安装和简单命令

   其实对于PG,自己总是听圈内人说和Oracle很相似,自己也有一些蠢蠢欲动学习的想法,从我的感觉来看,它是介于Oracle和MySQL之间的一种数据库,具备类似Oracle的功能,兼具MySQL的开源风格。所以个人感觉无论是从Oracle转向学习PG,还是从MySQL转向PG都会有一些独到的侧重方向。当然学习数据库本身不是目的,会的越多并不能说明你很牛,能够深入理解数据库,就如同感受的性格和处事风格,在合适的场景使用它,无想应该是超越于技术本身之外,而且难能可贵的。
  其实我本身也是一个浮躁的人,不喜欢全表扫描式的学习,很多东西都不喜欢按照那种系统的方式来学习,很多东西都想先问问,或者走捷径最好。如果一个坎绕过去了,我喜欢再绕回去反复走走。所以在快下班的时候,专门抽了不到一个小时的时间,在同事的帮助下完成了PG的安装。
  当然本来是想简单安装一下PG,简单了解一下,结果最后竟然尝试成功了源码安装。
如果网络允许,完全可以使用wget来下载,或者到官网离线下载。
#wget https://ftp.postgresql.org/pub/source/v9.5.2/postgresql-9.5.2.tar.gz
。。。
HTTP request sent, awaiting response... 200 OK
Length: 24100449 (23M) [application/x-gzip]
Saving to: `postgresql-9.5.2.tar.gz'

100%[========================>] 24,100,449  4.82M/s   in 7.0s   
2016-05-12 17:30:52 (3.26 MB/s) - `postgresql-9.5.2.tar.gz' saved [24100449/24100449]
得到了源码包之后,使用tar -zxvf的方式解压即可。
至于源码安装,真心比自己想象要简单很多,不过也遇到了一些小问题。
源码安装的步骤就是./configure  ; gmake ; gmake install三步
在第一步的时候发现有这么个错误。
checking for library containing sched_yield... none required
checking for library containing gethostbyname_r... none required
checking for library containing shmget... none required
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
看起来是readline的包缺少,但是查看rpm包是有的。
#rpm -qa|grep readli
readline-5.1-3.el5
readline-5.1-3.el5
还有一个是zlib包的同样警告,最后勉强使用./configure --without-readline --without-zlib的方式编译,终于成功了。当然这种方式会无法启用一些特性,readline我是知道的,应该是在命令中上下翻页的功能会失效。但是暂时不影响核心功能。
gmake; gmake install的步骤很快就完成了,最后以一句“PostgreSQL installation complete.”结束。

接下来就是创建用户,默认还是创建postgres的用户,要不可能要改动一些配置文件。
useradd postgres
然后把/usr/local/psql/bin放入环境变量中。
$which initdb
/usr/local/pgsql/bin/initdb
使用下面的方式来初始化,这个过程就如同MySQL的installdb一般。
$initdb -D /home/postgres/data --locale=C --encoding=UTF8
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

然后使用下面的方式来启动PG,当然可以配置到service中,可以使用pg_ctl来启动,方法确实比较多。
$postgres -D /home/postgres/data >/home/postgres/log/pg.log &2>1
完成之后就可以顺利进入PG的命令行界面了。
当然readline的功能确实是无法启用,我们回过头来看看到底是怎么回事。经过简单的排查,认为是缺少了readline-devel的包,使用yum来安装后。我们来清空编译,重新编译一次。
make clean
./configure
gmake
gmake install
当然这一次就非常顺利了,很快就进入了PG命令行界面。
我们来简单看看PG的进程,可以看到它也是有着多进程的方式,里面尤其是write process,checkpointer  process和Oracle中应该是类似的功能。
[postgres@iZu127ehmv7Z ~]$ps -ef|grep post
root     12928 24641  0 18:07 pts/0    00:00:00 su - postgres
postgres 12929 12928  0 18:07 pts/0    00:00:00 -bash
postgres 12953 12929  0 18:07 pts/0    00:00:00 postgres -D /home/postgres/data
postgres 12955 12953  0 18:07 ?        00:00:00 postgres: checkpointer process   
postgres 12956 12953  0 18:07 ?        00:00:00 postgres: writer process       
postgres 12957 12953  0 18:07 ?        00:00:00 postgres: wal writer process   
postgres 12958 12953  0 18:07 ?        00:00:00 postgres: autovacuum launcher process   
postgres 12959 12953  0 18:07 ?        00:00:00 postgres: stats collector process   
postgres 13020 12929  0 18:36 pts/0    00:00:00 ps -ef
postgres 13021 12929  0 18:36 pts/0    00:00:00 grep post

我们来简单用几个命令玩玩。
查看PG的版本
postgres=# SELECT version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54), 64-bit
(1 row)
查看连接信息。
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
查看数据字典,看看有哪些数据库。
postgres=# select datname from pg_database;
  datname  
-----------
 template1
 template0
 postgres
 test
(4 rows)
想看看使用的用户
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
看看当前时间,这些和MySQL是一样的。
postgres=# select now();
              now              
-------------------------------
 2016-05-12 19:44:35.337461+08
(1 row)
看看有哪些schemas
postgres=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)
创建一个数据库test,方式和MySQL一样。
create database test;
查看数据库的大小,可以发现是大小写敏感的。
STATEMENT:  SELECT pg_size_pretty(pg_database_size('TEST')) As fulldbsize;
ERROR:  database "TEST" does not exist
postgres=#   SELECT pg_size_pretty(pg_database_size('test')) As fulldbsize;
 fulldbsize
------------
 7224 kB
(1 row)
如果要连入test数据库
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=#
查看存在的数据库
test=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     |
(4 rows)
查看数据字典的信息
postgres=#  \dS+
                                     List of relations
   Schema   |              Name               | Type  |  Owner   |    Size    | Description
------------+---------------------------------+-------+----------+------------+-------------
 pg_catalog | pg_aggregate                    | table | postgres | 48 kB      |
 pg_catalog | pg_am                           | table | postgres | 40 kB      |
 pg_catalog | pg_amop                         | table | postgres | 80 kB      |
 pg_catalog | pg_amproc                       | table | postgres | 64 kB      |
 pg_catalog | pg_attrdef                      | table | postgres | 8192 bytes |
 pg_catalog | pg_attribute                    | table | postgres | 392 kB     |
 pg_catalog | pg_auth_members                 | table | postgres | 0 bytes    |
 pg_catalog | pg_authid                       | table | postgres | 40 kB      |

比如我们来看看pg_settings的字段情况,尝试使用desc
postgres=# desc pg_settings
postgres-#
竟然没有任何反应,原来是用\d的方式
postgres=# \d pg_settings
     View "pg_catalog.pg_settings"
     Column      |  Type   | Modifiers
-----------------+---------+-----------
 name            | text    |
 setting         | text    |
 unit            | text    |
 category        | text    |
。。。
查看最近执行的命令
\s
忍不住创建一个表试试
test=# create table test_tab(id int);
CREATE TABLE
从数据字典里查看表的信息
test=# select table_catalog,table_schema,table_name,table_type from information_schema.tables;
 table_catalog |    table_schema    |              table_name               | table_type
---------------+--------------------+---------------------------------------+------------
 test          | public             | test_tab                              | BASE TABLE
 test          | pg_catalog         | pg_statistic                          | BASE TABLE
 test          | pg_catalog         | pg_type                               | BASE TABLE
 test          | pg_catalog         | pg_authid                             | BASE TABLE
 test          | pg_catalog         | pg_roles                              | VIEW
 test          | pg_catalog         | pg_shadow                             | VIEW
查看表中字段的情况
test=# SELECT column_name FROM information_schema.columns WHERE table_name ='test_tab';
 column_name
-------------
 id
(1 row)
查看数据库test中的表
test=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner   
--------+----------+-------+----------
 public | test_tab | table | postgres
(1 row)






来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-2098920/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23718752/viewspace-2098920/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值