Linux ·

Linux下安装 PostgreSQL 并设置基本参数

Linux下安装Postgresql有二进制格式安装和源码安装两种安装方式,这里用的是二进制格式安装。各个版本的Linux都内置了Postgresql,所以可直接通过命令行安装便可。本文用的是CentOS 6.5。

安装Postgresql

# 安装postgresql服务器
yum install postgresql-server
#依赖包是否安装
Y
/N  Y

#第三方贡献包
yum install postgresql-contrib
#依赖包是否安装
Y
/N  Y

安装成功后,数据库状态

[root@localhost Hadoop]# service postgresql status
postmaster is stopped

尝试启动数据库,但报错,需要先初始化数据目录

[root@localhost hadoop]# service postgresql start
/var/lib/pgsql/data is missing.Use "service postgresql initdb" to initialize the cluster first.
[root@localhost hadoop]# service postgresql initdb
Initializing database:                                                [OK]

启动数据库

通过service命令启动Postgresql,需要注意的是,默认在安装时会创建postgres用户并安装到此用户下。而Postgresql 的默认数据库也是用此用户命名的。

[root@localhost hadoop]# service postgresql start
Starting postgresql service:                              [  OK  ]
[root@localhost hadoop]#
su - postgres
-bash-4.1$ psql
psql (
8.4.20)
Type
"help" for help.

postgres=# \l
                                  List of databases
  Name   
|  Owner  | Encoding |  Collation  |    Ctype    |  Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
postgres 
| postgres | UTF8    | en_US.UTF-8 | en_US.UTF-8 |
template0
| postgres | UTF8    | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                            : postgres
=CTc/postgres
template1
| postgres | UTF8    | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                            : postgres
=CTc/postgres
(
3 rows)

postgres=#

Postgresql 的Psql 就等于Oracle的Sqlplus一样 ,直接用命令Psql登录等于用操作系统验证登录,不需要输入用户名和密码。

基本参数设置

在Centos下,默认的数据目录在 /var/lib/pgsql/data 下 ,配置的参数文件就在此目录下。

-bash-4.1$ ls -l
total
80
drwx
------ 5 postgres postgres  4096 Nov 16 07:43 base
drwx
------ 2 postgres postgres  4096 Nov 17 23:51 global
drwx
------ 2 postgres postgres  4096 Nov 16 07:43 pg_clog
-rw------- 1 postgres postgres  3533 Nov 17 22:05 pg_hba.conf
-rw------- 1 postgres postgres  1631 Nov 16 07:43 pg_ident.conf
drwx
------ 2 postgres postgres  4096 Nov 18 00:00 pg_log
drwx
------ 4 postgres postgres  4096 Nov 16 07:43 pg_multixact
drwx
------ 2 postgres postgres  4096 Nov 18 00:00 pg_stat_tmp
drwx
------ 2 postgres postgres  4096 Nov 16 07:43 pg_subtrans
drwx
------ 2 postgres postgres  4096 Nov 16 07:43 pg_tblspc
drwx
------ 2 postgres postgres  4096 Nov 16 07:43 pg_twophase
-rw------- 1 postgres postgres    4 Nov 16 07:43 PG_VERSION
drwx
------ 3 postgres postgres  4096 Nov 16 07:43 pg_xlog
-rw------- 1 postgres postgres 16877 Nov 17 21:54 postgresql.conf
-rw------- 1 postgres postgres    57 Nov 17 23:51 postmaster.opts
-rw------- 1 postgres postgres    45 Nov 17 23:51 postmaster.pid

配置远程登录数据库

1. 修改 postgresql.conf 文件,配置PostgreSQL数据库服务器的相应的参数

listen_addresses = '*'       # PostgreSQL安装完成后,默认是只接受来在本机localhost的连接请求,此处将数据库服务器的监听模式修改为监听所有主机发出的连接请求
port = 5432      #  默认端口,修改后要重启数据库

2. 修改 pg_hba.conf 文件,配置对数据库的访问权限

image

在最后一行加上配置,表示允许网段192.168.191.0上的所有主机使用所有合��的数据库用户名访问数据库,

24是子网掩码,表示允许IP范围在 192.168.191.0--192.168.191.255 的计算机访问。

3. 测试远程登录

首先修改默认数据库用户登录密码

-bash-4.1$ psql
psql (8.4.20)
Type "help" for help.

postgres=# \password

按提示修改密码。

然后再从另一台局域网机器上登录

psql -U postgres -d postgres -h 192.168.191.5 -p 5432      -- 成功

其中 –u 指定用户,-d 指定数据库名 ,-h 指定host,-p 端口号,按提示输入密码。

 

另外,可视化客户端推荐用DBeaver。

image

-- 感谢您付出的阅读时间。

------------------------------------华丽丽的分割线------------------------------------

在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

------------------------------------华丽丽的分割线------------------------------------

参与评论