简介
PostgreSQL 是一个开源的对象-关系数据库服务器,有着极其丰富的数据类型的支持,并且支持对数据类型进行扩展。支持众多的开发语言。在PostGIS的扩展下提供了很强的空间信息服务功能。
本文介绍PostgreSQL的安装和基本用法,供初次使用者上手。
用Yum安装PostgreSQL数据库
首先需要在系统中添加官网提供的对应版本的Yum仓库,命令如下:
sudo yum install http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm
然后安装PostgreSQL数据库:
sudo yum install postgresql93-server postgresql93-contrib
配置PostgreSQL依赖的环境,然后启动数据库
使用postgres用户,进行PostgreSQL的日常管理,postgres为默认的管理用户名,比较方便,命令如下:
sudo mkdir -p /data/pgsql
sudo chown postgres:postgres /data/pgsql
sudo su - postgres
cp /etc/skel/.bash* /var/lib/pgsql
设置PGDATE环境变量,指向数据库物理文件的存放目录,比如存放在刚创建好的/data/pgsql
目录下,编辑/var/lib/pgsql/.bashrc
文件,在文件末尾添加下面代码块的内容:
#添加变量设置
export PGDATA=/data/pgsql
export PATH=/usr/pgsql-9.3/bin:$PATH
然后重新加载.bashrc
,使刚设置的环境变量生效:
现在就可以初始化数据库目录了,刚才我们已经将```/data/pgsql```目录的**owner**改为了```postgres```用户,命令如下:
[postgres@centos ~]$ initdb [48/901] 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 /data/pgsql ... ok creating subdirectories ... ok selecting default maxconnections ... 100 selecting default sharedbuffers ... 128MB creating configuration files ... ok creating template1 database in /data/pgsql/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects' descriptions ... ok creating collations ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok loading PL/pgSQL server-side language ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok syncing data to disk ... ok
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:
postgres -D /data/pgsql
or pg_ctl -D /data/pgsql -l logfile start
初始化数据存储目录的输出建议看一下,initdb命令输出了不少有用的信息,现在我们看下初始化之后的目录结构:
[postgres@centos ~]$ ls /data/pgsql/ base pgclog pgident.conf pgnotify pgsnapshots pgstattmp pgtblspc PGVERSION postgresql.conf global pghba.conf pgmultixact pgserial pgstat pgsubtrans pgtwophase pg_xlog
其中**postgresql.conf**和**pg_hba.conf**分别是主配置文件和权限管理配置文件,这一点(配置文件的位置)与mysql数据库非常不一样。接下来我们启动PostgreSQL实例:
[postgres@centos ~]$ pgctl start server starting [postgres@centos ~]$ < 2015-05-10 00:09:40.411 CST >LOG: redirecting log output to logging collector process < 2015-05-10 00:09:40.411 CST >HINT: Future log output will appear in directory "pglog".
输出显示:Postgres默认将日志保存在了```$PGDATA/pg_log```目录下面,现在看下日志都记录了些什么信息:
[postgres@centos ~]$ tail /data/pgsql/pg_log/postgresql-*.log < 2015-05-10 00:09:40.414 CST >LOG: database system was shut down at 2015-05-10 00:01:19 CST < 2015-05-10 00:09:40.434 CST >LOG: database system is ready to accept connections < 2015-05-10 00:09:40.434 CST >LOG: autovacuum launcher started
日志只是很简单的记录了数据库成功启动了,可以接受新的连接了,接下来就可以登录**PostgreSQL**了。
#登录和简单使用**PostgreSQL**数据库
登录
[postgres@centos ~]$ psql psql (9.3.6) Type "help" for help.
postgres=#
看到```postgres=#```提示符说明已经成功登录了数据库。执行```psql```不带任何参数意味着我们是以postgres用户的身份登录并默认使用了名为postgres的数据库;接下来执行一些简单的查询:
查看postgres默认带了哪些数据库
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | enUS.UTF-8 | enUS.UTF-8 | template0 | postgres | UTF8 | enUS.UTF-8 | enUS.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | enUS.UTF-8 | enUS.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)
查看postgres数据库带了哪些表,表在postgres数据库中被称为关系(relation):
postgres=# \d No relations found.
可见postgres数据库默认没有创建任何表。
```
关于怎么创建表和执行SQL进行增删改查,与其它数据库(比如:MySQL)一样,使用标准的SQL语句即可,本文档就不再演示了。
总结
本文档适合了解数据库基本概念的初学者使用,为了减少入门的疑惑没有过多提到PostgreSQL的专有名词,比如:数据存储目录(上文的/data/pgsql目录)叫cluster,用户和组叫role(上文的postgres用户),待成功安装并简单使用有了直观认识之后,有兴趣的可以参考官方文档(推荐)或Google进行更深入的学习。