温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PostgreSQL的安装和启动方法

发布时间:2021-08-30 19:02:03 来源:亿速云 阅读:185 作者:chen 栏目:关系型数据库

这篇文章主要介绍“PostgreSQL的安装和启动方法”,在日常操作中,相信很多人在PostgreSQL的安装和启动方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”PostgreSQL的安装和启动方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

官方文档

二进制安装和启动:

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

到此,关于“PostgreSQL的安装和启动方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI