`

【转】在CentOS上安装PostgreSQL数据库

阅读更多

一、在CentOS上安装PostgreSQL数据库 

下面我们就以CentOS系统为例,给大家讲解一下PostgreSQL的安装过程。 

1.使用yum安装postgresql 

 

[pengchengxiang@localhost ~]$ sudo yum install postgresql-server.x86_64 
Loaded plugins: fastestmirror, refresh-packagekit, security 
Setting up Install Process 
Loading mirror speeds from cached hostfile 
 * base: mirrors.btte.net 
 * extras: mirror.bit.edu.cn 
 * updates: mirror.bit.edu.cn 
Resolving Dependencies 
--> Running transaction check 
---> Package postgresql-server.x86_64 0:8.4.20-3.el6_6 will be installed 
--> Processing Dependency: postgresql(x86-64) = 8.4.20-3.el6_6 for package: postgresql-server-8.4.20-3.el6_6.x86_64 
--> Running transaction check 
---> Package postgresql.x86_64 0:8.4.20-3.el6_6 will be installed 
--> Finished Dependency Resolution 
Dependencies Resolved 
================================================================================ 
 Package                 Arch         Version               Repository     Size 
================================================================================ 
Installing: 
 postgresql-server       x86_64       8.4.20-3.el6_6        updates       3.4 M 
Installing for dependencies: 
 postgresql              x86_64       8.4.20-3.el6_6        updates       2.6 M 
Transaction Summary 
================================================================================ 
Install       2 Package(s) 
Total download size: 6.0 M 
Installed size: 28 M 
Is this ok [y/N]: y 
Downloading Packages: 
(1/2): postgresql-8.4.20-3.el6_6.x86_64.rpm              | 2.6 MB     00:02      
(2/2): postgresql-server-8.4.20-3.el6_6.x86_64.rpm       | 3.4 MB     00:06      
-------------------------------------------------------------------------------- 
Total                                           680 kB/s | 6.0 MB     00:09      
Running rpm_check_debug 
Running Transaction Test 
Transaction Test Succeeded 
Running Transaction 
  Installing : postgresql-8.4.20-3.el6_6.x86_64                             1/2  
  Installing : postgresql-server-8.4.20-3.el6_6.x86_64                      2/2  
  Verifying  : postgresql-8.4.20-3.el6_6.x86_64                             1/2  
  Verifying  : postgresql-server-8.4.20-3.el6_6.x86_64                      2/2  
Installed: 
  postgresql-server.x86_64 0:8.4.20-3.el6_6                                      
Dependency Installed: 
  postgresql.x86_64 0:8.4.20-3.el6_6                                             
Complete!

 

2.初始化postgresql数据库 

 

[pengchengxiang@localhost ~]$ sudo service postgresql initdb 
Initializing database: [  OK  ]

 

2.启动postgresql服务 

 

[pengchengxiang@localhost ~]$ sudo service postgresql start 
Starting postgresql service: [  OK  ]

 

3.查看postgresql的服务状态 

 

[pengchengxiang@localhost ~]$ sudo service postgresql status 
postmaster (pid  3496) is running...

 

问题:如果你在没有进行初始化数据库之前就启东postgrepsql服务,则会报错如下: 

 

[pengchengxiang@localhost ~]$ sudo service postgresql start 
/var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first. 
[FAILED]

 

二、连接PostgreSQL数据库 

如果想连接到数据库,需要切换到postgres用户下,然后使用psql连接到数据库中。在该用户下连接数据库,是不需要密码的。 

1.切换的postgres用户,并连接数据库 

 

[pengchengxiang@localhost ~]$ sudo su - postgres 
-bash-4.1$ psql 
psql (8.4.20) 
Type "help" for help. 
postgres=#

 

2.列出所有的数据库 

 

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)

 

3.退出数据库 

 

postgres=# \q 
-bash-4.1$

 

三、PostgreSQL数据库目录 

默认安装上,PostgreSQL的数据库目录在/var/lib/pgsql/data目录。 

 

-bash-4.1$ ls -l /var/lib/pgsql/data/ 
total 80 
drwx------. 5 postgres postgres  4096 Nov 16 23:55 base 
drwx------. 2 postgres postgres  4096 Nov 16 23:55 global 
drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_clog 
-rw-------. 1 postgres postgres  3411 Nov 16 23:55 pg_hba.conf 
-rw-------. 1 postgres postgres  1631 Nov 16 23:55 pg_ident.conf 
drwx------. 2 postgres postgres  4096 Nov 17 00:00 pg_log 
drwx------. 4 postgres postgres  4096 Nov 16 23:55 pg_multixact 
drwx------. 2 postgres postgres  4096 Nov 17 00:02 pg_stat_tmp 
drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_subtrans 
drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_tblspc 
drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_twophase 
-rw-------. 1 postgres postgres     4 Nov 16 23:55 PG_VERSION 
drwx------. 3 postgres postgres  4096 Nov 16 23:55 pg_xlog 
-rw-------. 1 postgres postgres 16886 Nov 16 23:55 postgresql.conf 
-rw-------. 1 postgres postgres    57 Nov 16 23:55 postmaster.opts 
-rw-------. 1 postgres postgres    45 Nov 16 23:55 postmaster.pid

 

四、PostgrepSQL的简单配置 

PostgreSQL数据库的配置主要是通过修改数据目录下的postgresql.conf文件来实现的。 

1.修改监听的ip和端口 

使用postgresql用户连接数据库后,进入到/var/lib/pgsql/data目录下,编辑postgresql.conf文件: 

 

# - Connection Settings - 
#listen_addresses = '*'                 # what IP address(es) to listen on; 
                                        # comma-separated list of addresses; 
                                        # defaults to 'localhost', '*' = all 
                                        # (change requires restart) 
#port = 5432                            # (change requires restart)

 

修改这两个参数之后,需要重启之后才能生效 

[pengchengxiang@localhost ~]$ sudo service postgresql restart 
Stopping postgresql service: [  OK  ] 
Starting postgresql service: [  OK  ]

ip和端口修改好,PostgreSQ 客户端连接可能还会出现如下问题 FATAL: no pg_hba.conf entry for host

要解决这个问题,只需要在PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”

在其下加上请求连接的机器IP

host all all 127.0.0.1/32 md5

32是子网掩码的网段;md5是密码验证方法,可以改(见文件pg_hba.conf上的说明)...

2.修改数据库log相关的参数 

日志收集,一般是打开的 

 

# This is used when logging to stderr: 
logging_collector = on                  # Enable capturing of stderr and csvlog 
                                        # into log files. Required to be on for 
                                        # csvlogs. 
                                        # (change requires restart)

 

日志目录,一般使用默认值 

 

# These are only used if logging_collector is on: 
log_directory = 'pg_log'                # directory where log files are written, 
                                        # can be absolute or relative to PGDATA

 

只保留一天的日志,进行循环覆盖 

 

log_filename = 'postgresql-%a.log'      # log file name pattern, 
                                        # can include strftime() escapes 
log_truncate_on_rotation = on           # If on, an existing log file of the 
                                        # same name as the new log file will be 
                                        # truncated rather than appended to. 
                                        # But such truncation only occurs on 
                                        # time-driven rotation, not on restarts 
                                        # or size-driven rotation.  Default is 
                                        # off, meaning append to existing files 
                                        # in all cases. 
log_rotation_age = 1d                   # Automatic rotation of logfiles will 
                                        # happen after that time.  0 disables. 
log_rotation_size = 0                   # Automatic rotation of logfiles will

 

3.内存参数的配置 

共享内存的大小,用于共享数据块。如果你的机器上有足够的内存,可以把这个参数改的大一些,这样数据库就可以缓存更多的数据块,当读取数据时,就可以从共享内存中读,而不需要再从文件上去读取。 

 

# - Memory - 
shared_buffers = 32MB                   # min 128kB 
                                        # (change requires restart)

 

单个SQL执行时,排序、hash json所用的内存,SQL运行完后,内存就释放了。 

 

# actively intend to use prepared transactions. 
#work_mem = 1MB                         # min 64kB

 

推荐教程:http://www.postgres.cn/docs/9.4/index.html

分享到:
评论

相关推荐

    CentOS安装Postgresql数据库

    ### CentOS安装PostgreSQL数据库 #### 一、PostgreSQL简介 ...通过以上步骤,你已经成功地在CentOS上安装并配置了PostgreSQL数据库管理系统。PostgreSQL的灵活性和强大功能使其成为处理复杂数据需求的理想选择。

    Centos6.8安装Postgresql11数据库.docx

    在本文中,我们将使用 CentOS 6.8 作为操作系统,安装 PostgreSQL 11 数据库。PostgreSQL 是一个功能强大且广泛使用的开源关系数据库管理系统。 二、安装存储库 RPM 为了安装 PostgreSQL 11,我们首先需要安装存储...

    Centos7安装postgresql依赖插件RPM包

    在Linux系统,特别是CentOS 7环境中安装PostgreSQL数据库时,常常需要先安装一系列依赖包以确保数据库能够正常运行。本篇文章将详细讲解在CentOS 7上安装PostgreSQL 12.5版本时,其关键依赖包RPM(Red Hat Package ...

    Centos7安装PostgreSql全自动部署脚本完整版

    ①添加【postgres】用户,数据库安装在postgres用户目录【/home/postgres/disk_d/applications/service/postgresql】路径下, ②给新用户授权 ③解压数据库文件 ④安装数据库依赖包 ⑤编译数据库 ⑥安装数据库 ⑦...

    PostGreSQL安装部署系列:Centos 7.9 安装指定PostGreSQL-15版本数据库

    通过本文所述步骤,您可以在CentOS 7.9上成功安装并配置PostgreSQL 15版本数据库。PostgreSQL的强大功能和高度的可扩展性使其成为众多应用场景的理想选择,无论是开发人员还是DBA,掌握其安装与配置流程都是十分必要...

    CentOS6平台下PostgreSQL数据库的安装

    在本文中,我们将深入探讨如何在CentOS 6平台上安装和配置PostgreSQL数据库,并了解如何在Windows环境下使用数据库管理工具pgAdmin访问这个数据库。PostgreSQL是一个开源的关系型数据库管理系统(RDBMS),它以其...

    在Centos7上搭建 PostgreSQL数据库主从复制

    在 Centos7 上搭建 PostgreSQL 数据库主从复制 在本篇文章中,我们将介绍如何在 Centos7 上搭建 PostgreSQL 数据库主从复制。 PostgreSQL 是一个功能强大且广泛使用的开源关系数据库管理系统。主从复制是 ...

    Linux centos7 postgresql12 离线安装包

    3. **安装依赖**:在安装PostgreSQL之前,确保系统已经安装了所需的依赖。可以运行以下命令检查和安装: ``` sudo yum install readline-devel libxml2-devel libxslt-devel zlib-devel openssl-devel gcc make `...

    使用Bucardo搭建PostgreSQL数据库双主同步.docx

    在编译 PostgreSQL 的过程中,需要带上 --with-perl 选项,以便正确地安装 Pl/PerlU 语言组件。 五、安装依赖包 在主节点上,需要安装以下依赖包: * DBI 模块 * DBD::Pg 模块 * DBIx::Safe 模块 其中,DBI 模块...

    postgresql14加postgis3.2在centos离线编译安装

    在Linux环境中,特别是CentOS操作系统上,离线安装软件包是一项常见的需求,特别是在没有网络连接或者网络环境受限的服务器上。本教程将详细介绍如何在CentOS系统上离线编译安装PostgreSQL 14和PostGIS 3.2,这两个...

    postgresql数据库在centos7下的安装

    postgresql数据库在centos7下的二进制安装。

    CentOS7 安装 PostgreSQL11的方法步骤

    在本文中,我们将详细探讨如何在CentOS 7系统上安装PostgreSQL 11,这是一个高度先进的开源关系型数据库管理系统。PostgreSQL以其强大的功能和对SQL标准的广泛支持而闻名,它提供了复杂查询、外键、触发器、视图、...

    基于linux的postgresql数据库部署以及PostGIS安装

    在本文中,我们将介绍如何在 Linux 操作系统上部署 PostgreSQL 数据库,并安装 PostGIS。PostgreSQL 是一个功能强大且开源的关系数据库管理系统,而 PostGIS 则是一个基于 PostgreSQL 的开源空间数据库扩展。 一、...

    nacos适配postgresql数据库

    1.nacos服务,适配postgresql数据库。 2.提供nacos,postgresql的创建nacos数据库脚本。 3.nacos/conf/nacos-pg.sql数据库脚本文件。 4.nacos版本1.4.2。

    CentOS7.4使用yum安装postgresql10.3并修改数据库存放位置

    史上最详细,可操作性最强postgresql安装文档,yum安装指定数据库安装位置,介绍安装步骤的操作原理,适合举一反三刨根问底的同学。

    postgresql V15 CentOS7 离线安装包 及 依赖包

    libzstd-1.5.5-1.el7.x86_64.rpm libicu-50.2-4.el7_7.x86_64.rpm postgresql15-15.3-1PGDG.rhel7.x86_64.rpm postgresql15-libs-15.3-1PGDG.rhel7....CentOS7 下 PostgreSQL15 离线安装 及 libzstd ,libicu 依赖包;

    centos7安装Postgresql(源码版)

    在本文中,我们将深入探讨如何在CentOS 7操作系统中安装PostgreSQL数据库管理系统,特别是采用源码方式进行安装。PostgreSQL是一种强大的开源关系型数据库系统,它具有高度的可扩展性和可靠性,广泛应用于企业级应用...

    centos操作系统安装postgresql+postgis部署配置

    可利用里面文件离线在linux(centos)操作系统中顺利安装部署,本资源可在半小时内完成各种配置,简单方便,并通过了几台服务器环境测试。 基于pg库进行地理空间数据存储的开源数据库技术方案,只有正确安装改库...

    PostgreSQL数据库在Windows和Linux系统下的安装

    综上所述,PostgreSQL数据库在Windows和Linux系统下的安装流程虽然略有差异,但基本原理相同,都是通过下载安装包、运行安装程序、配置相关参数、启动数据库服务来完成的。不过,在使用虚拟机时,还需额外设置虚拟...

    Centos 6的postgresql安装

    通过以上步骤,你可以在CentOS 6系统上成功安装并配置PostgreSQL 9.4.1数据库服务。此文档详细介绍了从安装依赖、配置安装路径、初始化数据库、设置用户密码、配置允许远程访问到防火墙设置的整个过程。确保每一步都...

Global site tag (gtag.js) - Google Analytics