PostgreSQL — 安装

目录

安装

# 安装 PG 服务器
yum install postgresql-server -y

# 安装 PG 客户端(可选)
yum install postgresql -y

注:在 CentOS7 上使用 YUM 安装 postgresql-server 会附带安装上 postgres 客户端,因此不必重复安装。

YUM 源 PostgreSQL 的版本是 9.2.24,安装完成后,相关的操作命令 psql、postgresql-setup 会添加到 /usr/bin 目录下,可以在命令行下直接使用。

$ psql --version
psql (PostgreSQL) 9.2.24

安装的同时还会创建 postgres 用户,Home 为 /var/lib/pgsql,需要切换到 postgres 用户下才可以通过 psql 访问数据库服务。

$ cat /etc/passwd | grep postgres
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

安装完成之后,不能直接启动数据库进程,需要先执行初始化。初始化过程会生成 PostgreSQL 的配置文件和存放数据库数据的数据库文件,路劲为 /var/lib/pgsql/data。

$ postgresql-setup initdb

$ l /var/lib/pgsql/data
总用量 48
drwx------ 5 postgres postgres    41 8月  20 18:15 base
drwx------ 2 postgres postgres  4096 8月  20 18:15 global
drwx------ 2 postgres postgres    18 8月  20 18:15 pg_clog
-rw------- 1 postgres postgres  4232 8月  20 18:15 pg_hba.conf
-rw------- 1 postgres postgres  1636 8月  20 18:15 pg_ident.conf
drwx------ 2 postgres postgres    58 8月  21 00:00 pg_log
drwx------ 4 postgres postgres    36 8月  20 18:15 pg_multixact
drwx------ 2 postgres postgres    18 8月  20 18:15 pg_notify
drwx------ 2 postgres postgres     6 8月  20 18:15 pg_serial
drwx------ 2 postgres postgres     6 8月  20 18:15 pg_snapshots
drwx------ 2 postgres postgres    25 8月  21 16:21 pg_stat_tmp
drwx------ 2 postgres postgres    18 8月  20 18:15 pg_subtrans
drwx------ 2 postgres postgres     6 8月  20 18:15 pg_tblspc
drwx------ 2 postgres postgres     6 8月  20 18:15 pg_twophase
-rw------- 1 postgres postgres     4 8月  20 18:15 PG_VERSION
drwx------ 3 postgres postgres    60 8月  20 18:15 pg_xlog
-rw------- 1 postgres postgres 19844 8月  20 18:15 postgresql.conf
-rw------- 1 postgres postgres    57 8月  20 18:15 postmaster.opts
-rw------- 1 postgres postgres    91 8月  20 18:15 postmaster.pid

启动服务,默认监听本机 127.0.0.1 的 5432 端口。

$ systemctl start postgresql

登录

默认的,只有在 CentOS 中使用 postgres 用户才可以通过 PostgreSQL 的 postgres 用户登录到 postgres 数据库。否则会触发错误:

$ psql postgres
psql: 致命错误:  角色 "root" 不存在
$ psql -U postgres
psql: 致命错误:  对用户"postgres"的对等认证失败
  • -U, --username=用户名 指定数据库用户名(缺省:“root”)

这是因为 PostgreSQL 实现了很多基于 Bash 的指令,这些指令显然只能在特定的用户下才能使用。

需要修改 pg_hba.conf 中的配置让 Root 用户和 postgres 用户是对等可信的:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust

另外,需要修改 postgresql.conf 中的 listen_addresses 配置:

#listen_addresses = 'localhost'     # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)
listen_addresses = '*'

并在 pg_hba.conf 中添加:

host    all    all    0.0.0.0/0    md5

重启服务生效:

systemctl restart postgresql.service

再次查看服务进程的监听端口,修改为了 any 0.0.0.0:

$ netstat -npltu | grep postgres
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      11451/postgres
tcp6       0      0 :::5432                 :::*                    LISTEN      11451/postgres

再次通过 Root 用户使用 psql -U postgres 进行登录:

$ psql postgres
psql: 致命错误:  角色 "root" 不存在
$ psql -U postgres
psql (9.2.24)
输入 "help" 来获取帮助信息.

postgres-# \l
                                     资料库列表
   名称    |  拥有者  | 字元编码 |  校对规则   |    Ctype    |       存取权限
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |
 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 行记录)

远程登录

注意,在 PostgreSQL Server 本地进行登录是不需要提供密码的,但远程登录需要。所以为了启用远程登录,首先需要设定用户的密码。有两种方式:

  1. 执行 \password 指令。
  2. 执行 alter user postgres with password ‘{your_pwd}’ SQL 语句。

默认的,PostgreSQL 内置了 postgres 数据库和 postgres 用户,所以我们已修改 postgres 用户的密码为例:

  1. 使用 postgres 账户登录到 postgres 数据库;
psql -d postgres -U postgres
  1. 执行 /password 指令,并输入 postgres 用户的密码。
postgres=# \password
输入新的密码:
再次键入:
postgres=# \q
  1. 修改密码后,使用 postgres 账户进行远程登录就要使用密码进行身份认证了。
$ psql --host=172.18.22.204 --port=5432 --dbname=postgres --username=postgres -W
用户 postgres 的口令:
psql: 致命错误:  用户 "postgres" Password 认证失败

创建新的用户和新的数据库

  • 创建用户
create user ‘{username}’ with password '{password}';
  • 创建数据库
create database ‘{database}’ owner ‘{username}’;
  • 将数据库权限全部赋给某个用户
grant all on database ‘{database}’ to ‘{username}’;
  • 通过文件导入数据库
psql -U ‘{username}’ ‘{database}’ < /data/dum.sql

猜你喜欢

转载自blog.csdn.net/Jmilk/article/details/108129556