语言
<< 返回文章列表

PostgreSQL的安装和启动方法大全

2020年1月7日
廖学强
338

官方文档


二进制安装和启动:


https://www.postgresql.org/download/linux/redhat/


源码安装和启动:

https://www.postgresql.org/docs/11/install-procedure.html


https://www.postgresql.org/docs/11/creating-cluster.html


https://www.postgresql.org/docs/11/server-start.html


https://www.postgresql.org/docs/current/kernel-resources.html


两种方法


1、二进制安装(linux是rpm包,windows是exe安装文件)


2、源码安装,推荐安装方式


二进制安装


示例:centos7_64平台,数据库postgresql11,使用yum安装


不使用yum的话可以直接使用rpm包安装,rpm包下载地址https://yum.postgresql.org/rpmchart.php


1、安装RPM的yum源,其实就是下载一个postgresql的yum源pgdg-redhat-all.repo文件到/etc/yum.repos.d目录下,有了这个yum源后,就可以直接yum install postgresql11安装postgresql数据库了


yum install

https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm


2、安装客户端,先执行search看有哪些PostgreSQL client可供安装


yum search 'PostgreSQL client'


yum install postgresql11


3、安装服务端,先执行search看有哪些PostgreSQL server可供安装,以下步骤安装好后,会自动创建用户postgres,自动创建目录/usr/pgsql-11


yum search 'PostgreSQL server'


yum install postgresql11-server


4、初始化数据库并启用开机自动启动


/usr/pgsql-11/bin/postgresql-11-setup initdb


systemctl enable postgresql-11


systemctl start postgresql-11


5、查看postgresql的进程


[root@zabbixtest2 ~]# ps -ef|grep postgres


postgres  1527     1  0 01:13 ?        00:00:00 /usr/pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/


6、连接postgresql数据库


su - postgres


psql -l


psql -d postgres


源码安装


1、建立postgresql用户、内核资源的配置,主要涉及/etc/security/limits.conf、/etc/sysctl.conf文件,类似oracle也需要配置这两个文件


2、解压tar包,进入解压目录,使用root用户执行如下,最后的make install命令把软件安装到/postgresql/pgsql目录


./configure --prefix=/postgresql/pgsql


make


make install


3、把软件安装目录/postgresql/pgsql授权宿主用户为postgresql


4、编辑postgresql用户的.bash_profile文件,PATH= /postgresql/pgsql/bin :$PATH、LD_LIBRARY_PATH= /postgresql/pgsql /lib


5、初始化数据库,后面两条命令任意一条都可以


su - postgresql


initdb -D /postgresql/pgsql/data


pg_ctl -D /postgresql/pgsq l/data initdb


6、启动postgresql程序,下面任意一个都可以,官方文档建议使用pg_ctl


postgres -D /postgresql/pgsql/data >logfile 2>&1 &


pg_ctl start -D /postgresql/pgsql/data -l logfile


7、查看postgresql的进程


[root@zabbixtest1 ~]# ps -ef|grep postgres


postgre+   803     1  0 07:10 pts/0    00:00:00 /postgresql/pgsql/bin/postgres -D /postgresql/pgsql/data


8、连接postgresql数据库,指定连接postgres库


psql -l


psql -d postgres


备注 :psql命令不加端口和不加数据库名,表示默认进入端口为5432并且数据库名和初始数据库initdb时的用户名一样的数据库,比如初始数据库initdb时的用户名为A,则默认进入A库。postgresql的默认端口是5432,默认数据库是postgres、template0、template1


如果此时端口不是5432,则会报错psql: FATAL:  role "A" does not exist


因为安装用户"A"对应的实例端口为其他,端口5432数据库的Owner不是"A"


如果此时没有"A"库,则会报错psql: FATAL:  database "A" does not exist


因为安装用户"A",默认进入"A"库,而"A"库是不存在的


9、创建一个名为test的数据库


createdb test

10、查看数据库状态


pg_ctl status -D /postgresql/pgsql/data


11、关闭数据库


pg_ctl stop -D /postgresql/pgsql/data


官方文档Short Version简要步骤


./configure


make


su


make install


adduser postgres


mkdir /usr/local/pgsql/data


chown postgres /usr/local/pgsql/data


su - postgres


/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data


/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &


/usr/local/pgsql/bin/createdb test


/usr/local/pgsql/bin/psql test


出处:http://blog.itpub.net/30126024/viewspace-2648733/