阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 6.5下PostgreSQL服务部署

144次阅读
没有评论

共计 8383 个字符,预计需要花费 21 分钟才能阅读完成。

PostgreSQL 是一种非常复杂的对象 - 关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统。

os:CentOS 6.5 x64
ip:192.168.85.130
hostname: vm2.lansgg.com
pg 版本:postgresql-9.2.4.tar.bz2

一、yum 安装
二、源码安装
三、系统数据库

1、yum 安装
[root@vm2 ~]# wget 
[root@vm2 ~]# rpm -vhi pgdg-RedHat92-9.2-8.noarch.rpm
[root@vm2 ~]# yum install postgresql92-server postgresql92-contrib -y

1.2、初始化并启动数据库
[root@vm2 ~]# /etc/init.d/postgresql-9.2 initdb
正在初始化数据库:[确定]
[root@vm2 ~]# /etc/init.d/postgresql-9.2 start
启动 postgresql-9.2 服务:[确定]

[root@vm2 ~]# echo “PATH=/usr/pgsql-9.2/bin:$PATH” >> /etc/profile
[root@vm2 ~]# echo “export PATH” >> /etc/profile

1.3、测试
[root@vm2 ~]# su – postgres
-bash-4.1$ psql
psql (9.2.19)
输入 “help” 来获取帮助信息.
 
postgres=# \l
                                    资料库列表
  名称    |  拥有者  | 字元编码 |  校对规则  |    Ctype    |      存取权限       
———–+———-+———-+————-+————-+———————–
 postgres  | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
          |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
          |          |          |            |            | postgres=CTc/postgres
(3 行记录)
 
postgres=#

1.4、修改管理员密码

修改 PostgreSQL 数据库用户 postgres 的密码(注意不是 linux 系统帐号)
PostgreSQL 数据库默认会创建一个 postgres 的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’。
postgres=# select * from pg_shadow;
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig 
———-+———-+————-+———-+———–+———+——–+———-+———–
 postgres |      10 | t          | t        | t        | t      |        |          | 
(1 行记录)
 
postgres=#  ALTER USER postgres WITH PASSWORD ‘postgres’;
ALTER ROLE
postgres=# select * from pg_shadow;
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |              passwd                | valuntil | useconfig 
———-+———-+————-+———-+———–+———+————————————-+———-+———–
 postgres |      10 | t          | t        | t        | t      | md53175bce1d3201d16594cebf9d7eb3f9d |          | 
(1 行记录)
 
postgres=#

1.5、创建测试数据库
postgres=# create database testdb;
CREATE DATABASE
postgres=# \c testdb;
您现在已经连线到数据库 “testdb”, 用户 “postgres”.
testdb=#

1.6、创建测试表
testdb=#  create table test (id integer, name text);
CREATE TABLE
testdb=# insert into test values(1,’lansgg’);
INSERT 0 1
testdb=# select * from test;
 id |  name 
—-+——–
  1 | lansgg
(1 行记录)

1.7、查看表结构
testdb=# \d test;
  资料表 “public.test”
 栏位 |  型别  | 修饰词 
——+———+——–
 id  | integer | 
 name | text    |

1.8、修改 PostgresSQL 数据库配置实现远程访问
修改 postgresql.conf 文件
如果想让 PostgreSQL 监听整个网络的话,将 listen_addresses 前的 #去掉,并将 listen_addresses = ‘localhost’ 改成 listen_addresses = ‘*’
修改客户端认证配置文件 pg_hba.conf
[root@vm2 ~]# vim /var/lib/pgsql/9.2/data/pg_hba.conf
host    all            all            127.0.0.1/32            ident
host    all            all            all                    md5

[root@vm2 ~]# /etc/init.d/postgresql-9.2 restart
停止 postgresql-9.2 服务:[确定]
启动 postgresql-9.2 服务:[确定]
[root@vm2 ~]#

2、源码安装
停止上面 yum 安装的 pgsql 服务,下载 PostgreSQL 源码包
[root@vm2 ~]# /etc/init.d/postgresql-9.2 stop
停止 postgresql-9.2 服务:[确定]
[root@vm2 ~]# wget 
[root@vm2 ~]# tar jxvf postgresql-9.2.4.tar.bz2
[root@vm2 ~]# cd postgresql-9.2.4

查看 INSTALL 文件
more INSTALL
INSTALL 文件中 Short Version 部分解释了如何安装 PostgreSQL 的命令,Requirements 部分描述了安装 PostgreSQL 所依赖的 lib,比较长,先 configure 试一下,如果出现 error,那么需要检查是否满足了 Requirements 的要求。
开始编译安装 PostgreSQL 数据库。
[root@vm2 postgresql-9.2.4]# ./configure
[root@vm2 postgresql-9.2.4]# gmake
[root@vm2 postgresql-9.2.4]# gmake install
[root@vm2 postgresql-9.2.4]# echo “PGHOME=/usr/local/pgsql” >> /etc/profile
[root@vm2 postgresql-9.2.4]# echo “export PGHOME” >> /etc/profile
[root@vm2 postgresql-9.2.4]# echo “PGDATA=/usr/local/pgsql/data” >> /etc/profile
[root@vm2 postgresql-9.2.4]# echo “export PGDATA” >> /etc/profile
[root@vm2 postgresql-9.2.4]# echo “PATH=$PGHOME/bin:$PATH” >> /etc/profile
[root@vm2 postgresql-9.2.4]# echo “export PATH” >> /etc/profile
[root@vm2 postgresql-9.2.4]# source /etc/profile
[root@vm2 postgresql-9.2.4]#

2.2、初始化数据库
useradd -d /opt/postgres postgres              ###### 如果没有此账户就创建,前面 yum 安装的时候已经替我们创建了
[root@vm2 postgresql-9.2.4]# mkdir /usr/local/pgsql/data
[root@vm2 postgresql-9.2.4]# chown postgres.postgres /usr/local/pgsql/data/
[root@vm2 postgresql-9.2.4]# su – postgres
 
-bash-4.1$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
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 “zh_CN.UTF-8”.
The default database encoding has accordingly been set to “UTF8”.
initdb: could not find suitable text search configuration for locale “zh_CN.UTF-8”
The default text search configuration will be set to “simple”.
 
fixing permissions on existing directory /usr/local/pgsql/data … ok
creating subdirectories … ok
selecting default max_connections … 100
selecting default shared_buffers … 32MB
creating configuration files … ok
creating template1 database in /usr/local/pgsql/data/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
 
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:
 
    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
 
-bash-4.1$

1.3、添加到系统服务
-bash-4.1$ exit
logout
[root@vm2 postgresql-9.2.4]#  cp /root/postgresql-9.2.4/contrib/start-scripts/linux /etc/init.d/postgresql
[root@vm2 postgresql-9.2.4]# chmod +x /etc/init.d/postgresql
[root@vm2 postgresql-9.2.4]# /etc/init.d/postgresql start
Starting PostgreSQL: ok
[root@vm2 postgresql-9.2.4]# chkconfig –add postgresql
[root@vm2 postgresql-9.2.4]# chkconfig postgresql on
[root@vm2 postgresql-9.2.4]#

1.4、测试使用
[root@vm2 postgresql-9.2.4]# su – postgres
-bash-4.1$ psql -l
                                  List of databases
  Name    |  Owner  | Encoding |  Collate  |    Ctype    |  Access privileges   
———–+———-+———-+————-+————-+———————–
 postgres  | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | 
 template0 | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
          |          |          |            |            | postgres=CTc/postgres
 template1 | postgres | UTF8    | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
          |          |          |            |            | postgres=CTc/postgres
(3 rows)
 
-bash-4.1$ psql
psql (9.2.4)
Type “help” for help.
 
postgres=# create database testdb;
CREATE DATABASE
postgres=# \c testdb;
You are now connected to database “testdb” as user “postgres”.
testdb=# create table test(id int,name text,age int);
CREATE TABLE
testdb=# \d test
    Table “public.test”
 Column |  Type  | Modifiers 
——–+———+———–
 id    | integer | 
 name  | text    | 
 age    | integer | 
 
testdb=# insert into test values(1,’lansgg’,25);
INSERT 0 1
testdb=# select * from test;
 id |  name  | age 
—-+——–+—–
  1 | lansgg |  25
(1 row)
 
testdb=#

3、系统数据库

在创建数据集簇之后,该集簇中默认包含三个系统数据库 template1、template0 和 postgres。其中 template0 和 postgres 都是在初始化过程中从 template1 拷贝而来的。

template1 和 template0 数据库用于创建数据库。PostgreSQL 中采用从模板数据库复制的方式来创建新的数据库,在创建数据库的命令中可以用“-T”选项来指定以哪个数据库为模板来创建新数据库。

template1 数据库是创建数据库命令默认的模板,也就是说通过不带“-T”选项的命令创建的用户数据库是和 template1 一模一样的。template1 是可以修改的,如果对 template1 进行了修改,那么在修改之后创建的用户数据库中也能体现出这些修改的结果。template1 的存在允许用户可以制作一个自定义的模板数据库,在其中用户可以创建一些应用需要的表、数据、索引等,在日后需要多次创建相同内容的数据库时,都可以用 template1 作为模板生成。

由于 template1 的内容有可能被用户修改,因此为了满足用户创建一个“干净”数据库的需求,PostgreSQL 提供了 template0 数据库作为最初始的备份数据,当需要时可以用 template0 作为模板生成“干净”的数据库。

而第三个初始数据库 postgres 用于给初始用户提供一个可连接的数据库,就像 Linux 系统中一个用户的主目录一样。

上述系统数据库都是可以删除的,但是两个模板数据库在删除之前必须将其在 pg_database 中元组的 datistemplate 属性改为 FALSE,否则删除时会提示“不能删除一个模板数据库”

———————————— 华丽丽的分割线 ————————————

在 CentOS 6.5 上编译安装 PostgreSQL 9.3 数据库 http://www.linuxidc.com/Linux/2016-06/132272.htm

CentOS 6.3 环境下 yum 安装 PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htm

PostgreSQL 缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htm

Windows 平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htm

Ubuntu 下 LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htm

Ubuntu 上的 phppgAdmin 安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htm

CentOS 平台下安装 PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htm

PostgreSQL 配置 Streaming Replication 集群 http://www.linuxidc.com/Linux/2014-05/101724.htm

———————————— 华丽丽的分割线 ————————————

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-01/139144.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计8383字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中