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

时间:2024-04-21 18:33:34

在Linux下安装Postgresql有二进制格式安装和源码安装两种安装方式,这里用的是二进制格式安装。各个版本的Linux都内置了Postgresql,所以可直接通过命令行安装便可。本文用的是Centos6.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.)
Type "help" for help. postgres=# \l
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF- | en_US.UTF- |
template0 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres
: postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF- | en_US.UTF- | =c/postgres
: postgres=CTc/postgres
( rows) postgres=#

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

基本参数设置


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

-bash-4.1$ ls -l
total
drwx------ postgres postgres Nov : base
drwx------ postgres postgres Nov : global
drwx------ postgres postgres Nov : pg_clog
-rw------- postgres postgres Nov : pg_hba.conf
-rw------- postgres postgres Nov : pg_ident.conf
drwx------ postgres postgres Nov : pg_log
drwx------ postgres postgres Nov : pg_multixact
drwx------ postgres postgres Nov : pg_stat_tmp
drwx------ postgres postgres Nov : pg_subtrans
drwx------ postgres postgres Nov : pg_tblspc
drwx------ postgres postgres Nov : pg_twophase
-rw------- postgres postgres Nov : PG_VERSION
drwx------ postgres postgres Nov : pg_xlog
-rw------- postgres postgres Nov : postgresql.conf
-rw------- postgres postgres Nov : postmaster.opts
-rw------- postgres postgres Nov : postmaster.pid

配置远程登录数据库

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

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

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

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

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

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

3. 测试远程登录

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

-bash-4.1$ psql
psql (8.4.)
Type "help" for help. postgres=# \password

按提示修改密码。

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

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

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

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

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