centos7上安装postgresql

 

获取源码

安装PostgreSQL的方法有很多,我们可以使用centos下的包管理器安装,也可以从官网下载二进制压缩包安装,但是很多时候包管理器的仓库和官网的二进制压缩包提供的版本过少,不能完全满足我们的需求。这时候我们可以选择从官网下载源码,自己编译安装。
从官网下载PostgreSQL源代码 https://www.postgresql.org/download/,选择source选项,下载对应版本的源码压缩包即可,根据自己的喜爱,可以选择下载tar.gz格式或者tar.bz2格式的压缩包。

前期准备

在编译之前,我们需要先新增一个操作pg的用户postgres,以后所有关于数据库的操作都会使用这个用户。

#添加新的用户组
groupadd develop
useradd -d /home/postgres -m -g develop postgres
#为用户增加密码
passwd postgres

编译

安装过程第一步,我们要配置一些安装信息,通过解压后文件夹里的configure文件实现的。如果我们想采用默认的安装,只需要执行这个脚本即可,如果我们想自定义安装过程,那么我们在执行脚本的时候需要加上一下参数,部分参数如下:

  • --prefix=PREFIX_DIR: 加上这个参数,pg会把所有的编译后的文件放到这个文件夹中。默认情况下,系统会把这个值设置成usr/local/pgsql。所以我们要是想把pg安装到其他文件夹下,只需要在执行脚本的时候加上这个参数即可。(一般来说,自定义安装只需要用到这个选项)
  • --exec-prefix=EXEC-PREFIX:可以通过该选项,将相关的文件安装到EXEC-PREFIX文件夹下,而不使用--prefix设置的地方。这样做可以比较方便地在不同主机之间共享体系相关的文件。
  • --bindir=DIRECTORY:可执行程序的目录,如果不指定,默认是EXEC-PREFIX/bin,如果使用默认的--exec-prefix选项,则默认是PREFIX_DIR/bin
  • --sysconfdir=DIRECTORY:各种配置文件的目录,默认是PREFIX_DIR/etc
./configure --prefix=/home/postgres/pgsql

上面例子,会把编译后的文件全部放到/home/postgres/pgsql目录下,包括可执行程序等文件也会放到这个目录下。
执行问configure脚本后,我们就可以执行 make命令执行源码。在执行源码之前我们需要检查是否有c编译器,如果没有,我们可以执行下面命令进行安装。

yum -y install gcc

有时候一些系统还缺少readlinezlib库,这时候我们还要执行下面命令安装。

yum -y install readline-devel
yum install zlib-devel

安装缺失的包之后就可以编译源码,安装。依次执行以下命令:

#这里选择将pg安装到这个目录下面,指定了--prefix之后,pg安装后文件会全部放到这里
./configure --prefix=/home/postgres/pgsql
make
make install

用户环境变量

为之前新建的用户添加环境变量,这样该用户就能够直接执行命令去操作pg,而不是使用绝对路径去操作命令。打开新建用户的.bash_profile文件并在文件后面加入以下内容:

#表示pg安装的目录,和--prefix的目录一致
PGHOME=/home/postgres/pgsql
export PGHOME
#pg数据目录,在初始化数据库时如果没有指定目录,则选择环境变量中的目录
PGDATA=/home/postgres/pgsql/data
export PGDATA

PATH=$PATH:$HOME/bin:$PGHOME/bin
export PATH

修改完执行命令使配置生效:

source .bash_profile

初始化数据库

由于配置了环境变量,所以此处我们直接执行initdb即可完成pg初始化。我们先键入initdb --help看一下命令相关的参数信息:

tomcat@hw-hadoop1-> initdb --help
initdb initializes a PostgreSQL database cluster.

Usage:
  initdb [OPTION]... [DATADIR]

Options:
  -A, --auth=METHOD         default authentication method for local connections
      --auth-host=METHOD    default authentication method for local TCP/IP connections
      --auth-local=METHOD   default authentication method for local-socket connections
 [-D, --pgdata=]DATADIR     location for this database cluster
  -E, --encoding=ENCODING   set default encoding for new databases
      --locale=LOCALE       set default locale for new databases
      --lc-collate=, --lc-ctype=, --lc-messages=LOCALE
      --lc-monetary=, --lc-numeric=, --lc-time=LOCALE
                            set default locale in the respective category for
                            new databases (default taken from environment)
      --no-locale           equivalent to --locale=C
      --pwfile=FILE         read password for the new superuser from file
  -T, --text-search-config=CFG
                            default text search configuration
  -U, --username=NAME       database superuser name
  -W, --pwprompt            prompt for a password for the new superuser
  -X, --xlogdir=XLOGDIR     location for the transaction log directory

Less commonly used options:
  -d, --debug               generate lots of debugging output
  -k, --data-checksums      use data page checksums
  -L DIRECTORY              where to find the input files
  -n, --noclean             do not clean up after errors
  -N, --nosync              do not wait for changes to be written safely to disk
  -s, --show                show internal settings
  -S, --sync-only           only sync data directory

Other options:
  -V, --version             output version information, then exit
  -?, --help                show this help, then exit

通过这些参数后面的介绍,我们可以在数据库初始化的时候指定数据目录、编码和日志等信息。

#初始化时指定了数据库的编码和数据目录
initdb  -E utf8 -D /home/postgres/pgsql/data

**postgresql不能用root用户操作,所以我们要切换到前面创建的postgres用户

若是切换用户时出现 -bash-4.2$:请参考:https://www.cnblogs.com/luxj/p/7654974.html

启动和连接

在初始化数据库之后我们要考虑启动数据库,pg则是使用pg_ctl命令来控制数据库的启动和关闭。

postgres@hw-hadoop1-> pg_ctl --help
pg_ctl is a utility to initialize, start, stop, or control a PostgreSQL server.

Usage:
  pg_ctl init[db]               [-D DATADIR] [-s] [-o "OPTIONS"]
  pg_ctl start   [-w] [-t SECS] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]
  pg_ctl stop    [-W] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
  pg_ctl restart [-w] [-t SECS] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
                 [-o "OPTIONS"]
  pg_ctl reload  [-D DATADIR] [-s]
  pg_ctl status  [-D DATADIR]
  pg_ctl promote [-D DATADIR] [-s]
  pg_ctl kill    SIGNALNAME PID

Common options:
  -D, --pgdata=DATADIR   location of the database storage area
  -s, --silent           only print errors, no informational messages
  -t, --timeout=SECS     seconds to wait when using -w option
  -V, --version          output version information, then exit
  -w                     wait until operation completes
  -W                     do not wait until operation completes
  -?, --help             show this help, then exit
(The default is to wait for shutdown, but not for start or restart.)

If the -D option is omitted, the environment variable PGDATA is used.

Options for start or restart:
  -c, --core-files       allow postgres to produce core files
  -l, --log=FILENAME     write (or append) server log to FILENAME
  -o OPTIONS             command line options to pass to postgres
                         (PostgreSQL server executable) or initdb
  -p PATH-TO-POSTGRES    normally not necessary

Options for stop or restart:
  -m, --mode=MODE        MODE can be "smart", "fast", or "immediate"

从帮助文档我们可以看到,使用pg_ctl命令可以帮我们启动、重启和关闭数据库。同时我们可以在启动的时候使用-D来指定数据库数据的目录,在下面的介绍我们可以看到,如果我们没有指定这个选项,那么就会使用环境变量中配置的目录,也就是我们之前在环境变量中配置PGDATA;我们也可以使用-l选项来指定日志的存储地点,以便我们方便查看数据库的日志信息。
启动数据库之后我们就可以使用psql命令连入数据库中,进入数据库我们首先要做的就是设置postgres用户的密码,键入\password,输入密码即可。
做完这些基本上就能够使用了,但是这样只能够允许我们在本机使用数据库,其他的网络是不能够连接到我们数据库的,所以我们要设置数据库允许远程访问。

**若是执行打开命令"pg_ctl start -l /var/lib/pgsql/initdb.log"打开时报错:

psql: could not connect to server: No such file or directory
 Is the server running locally and accepting
 connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

请参考:https://blog.csdn.net/weixin_37791303/article/details/86707614

  • 修改postgresql.conf文件
    找到安装目录下的postgresql.conf文件,并且在文件中找到listen_addresses选项,将其改成如下所示内容:
listen_addresses = '*'
  • 修改pg_hba.conf文件
    在与postgresql.conf文件相同的目录下找到该配置文件,打开该文件,编辑或添加下面一行。(也可以改成你允许连接的ip)
# 不限制连接的ip
host  all  all 0.0.0.0/0 md5

或者

# 限制连接的ip
host    all             all             127.0.0.1/24       trust




重启数据库即可。

猜你喜欢

转载自blog.csdn.net/qq_38279833/article/details/102551009