CentOS/RHEL Linux安装Postgresql数据库服务器

时间:2020-01-09 10:40:12  来源:igfitidea点击:

问题描述:如何在Red Hat Enterprise Linux 5/CentOS 5/Fedora Linux下安装和配置Postgresql数据库服务器?

解决方法:postgresql-server软件包包括创建和运行PostgreSQL服务器所需的程序,这将允许您创建和维护PostgreSQL数据库。

PostgreSQL是一种高级的对象关系数据库管理系统(DBMS),它支持几乎所有SQL构造(包括事务,子选择以及用户定义的类型和功能)。
如果要创建和维护自己的PostgreSQL数据库和/或自己的PostgreSQL服务器,则应安装postgresql-server。
您还需要安装postgresql软件包。

您还需要安装名为postgrsql的客户端软件包。
该软件包包含整个软件包的HTML文档,以及用于在PostgreSQL服务器上管理PostgreSQL数据库的命令行实用程序。

安装Postgrsql服务器

以root用户身份登录并输入命令:

# yum install postgresql postgresql-server

输出:

Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
Reading repository metadata in from local files
Parsing package install arguments
Resolving Dependencies
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for postgresql-server to pack into transaction set.
postgresql-server-8.1.9-1 100% |=========================|  87 kB    00:00     
---> Package postgresql-server.i386 0:8.1.9-1.el5 set to be updated
--> Running transaction check
--> Processing Dependency: postgresql = 8.1.9-1.el5 for package: postgresql-server
--> Restarting Dependency Resolution with new changes.
--> Populating transaction set with selected packages. Please wait.
---> Downloading header for postgresql to pack into transaction set.
postgresql-8.1.9-1.el5.i3 100% |=========================| 119 kB    00:00     
---> Package postgresql.i386 0:8.1.9-1.el5 set to be updated
--> Running transaction check

Dependencies Resolved

=============================================================================
Package                 Arch       Version          Repository        Size 
=============================================================================
Installing:
postgresql-server       i386       8.1.9-1.el5      updates           4.0 M
Installing for dependencies:
postgresql              i386       8.1.9-1.el5      updates           2.8 M

Transaction Summary
=============================================================================
Install      2 Package(s)         
Update       0 Package(s)         
Remove       0 Package(s)         

Total download size: 6.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): postgresql-server- 100% |=========================| 4.0 MB    00:05     
(2/2): postgresql-8.1.9-1 100% |=========================| 2.8 MB    00:04     
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing: postgresql                   ######################### [1/2] 
  Installing: postgresql-server            ######################### [2/2] 

Installed: postgresql-server.i386 0:8.1.9-1.el5
Dependency Installed: postgresql.i386 0:8.1.9-1.el5
Complete!

启动Postgresql

执行以下两个命令:

# chkconfig postgresql on
# service postgresql start

输出:

Initializing database:                                     [  OK  ]
Starting postgresql service:                               [  OK  ]

连接到Postgresql服务器

使用连接到服务器

# su - postgres

使用psql命令行工具进行连接:

$ psql -d template1 -U postgres

输出:

Welcome to psql 8.1.9, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

打开TCP端口5432

最后确保iptables允许远程访问Postgresql数据库服务器:打开/etc/sysconfig/iptables文件:

# vi /etc/sysconfig/iptables

在COMMIT行之前添加以下行以打开端口5432:

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

保存并关闭文件。
重新启动防火墙:

# service iptables restart

另请参见:PostgreSQL添加或创建用户帐户并授予数据库权限