PostgreSQL的安装和启动方法

31次阅读
没有评论

共计 3020 个字符,预计需要花费 8 分钟才能阅读完成。

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

官方文档

二进制安装和启动:

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 的安装和启动方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-28发表,共计3020字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)