咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

Ubuntu安装PostgreSQL

Ubuntu默认安装的PostgreSQL版本都是比较低的(差很多),通过官方源来安装最新的版本。

一、安装

参考【Linux downloads (Ubuntu) 】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get -y install postgresql
$ sudo service postgresql status
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Wed 2022-05-25 10:18:58 CST; 18min ago
Main PID: 4020835 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4578)
Memory: 0B
CGroup: /system.slice/postgresql.service

May 25 10:18:58 cnubuntu systemd[1]: Starting PostgreSQL RDBMS...
May 25 10:18:58 cnubuntu systemd[1]: Finished PostgreSQL RDBMS.

二、查看版本

1
2
3
$ sudo -u postgres psql -c 'SELECT version();'

PostgreSQL 14.3 (Ubuntu 14.3-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit

三、客户端命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# postgres是默认的超级用户,默认没有密码。
$ sudo -u postgres psql
psql (14.3 (Ubuntu 14.3-1.pgdg20.04+1))
Type "help" for help.

# 这里可以执行SQL命令
postgres=#

# 创建一个数据库mytest
postgres=# CREATE DATABASE mytest;
CREATE DATABASE

# 创建一个用户(ROLE)
postgres=# CREATE USER test;
CREATE ROLE

# 创建一个用户(ROLE)带密码
postgres=# CREATE USER demo password '123456';
CREATE ROLE

# 退出
postgres-# \q

四、认证

PostgreSQL提供多种不同的客户端认证方式,而 /etc/postgresql/14/main/pg_hba.conf 就是认证的配置文件,pg_hbaPostGreSQL host-base authentication 的简称。
参考【第 21 章 客户端认证】可以做出很多方式的认证,这里作为测试,信任所有来源,勿在生产环境这样设置。

这是默认配置 pg_hba.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file. A short
# synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access. Records take one of these forms:
#
# local DATABASE USER METHOD [OPTIONS]
# host DATABASE USER ADDRESS METHOD [OPTIONS]
# hostssl DATABASE USER ADDRESS METHOD [OPTIONS]
# hostnossl DATABASE USER ADDRESS METHOD [OPTIONS]
# hostgssenc DATABASE USER ADDRESS METHOD [OPTIONS]
# hostnogssenc DATABASE USER ADDRESS METHOD [OPTIONS]
#
# (The uppercase items must be replaced by actual values.)
#
# The first field is the connection type:
# - "local" is a Unix-domain socket
# - "host" is a TCP/IP socket (encrypted or not)
# - "hostssl" is a TCP/IP socket that is SSL-encrypted
# - "hostnossl" is a TCP/IP socket that is not SSL-encrypted
# - "hostgssenc" is a TCP/IP socket that is GSSAPI-encrypted
# - "hostnogssenc" is a TCP/IP socket that is not GSSAPI-encrypted
#
# DATABASE can be "all", "sameuser", "samerole", "replication", a
# database name, or a comma-separated list thereof. The "all"
# keyword does not match "replication". Access to replication
# must be enabled in a separate record (see example below).
#
# USER can be "all", a user name, a group name prefixed with "+", or a
# comma-separated list thereof. In both the DATABASE and USER fields
# you can also write a file name prefixed with "@" to include names
# from a separate file.
#
# ADDRESS specifies the set of hosts the record matches. It can be a
# host name, or it is made up of an IP address and a CIDR mask that is
# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that
# specifies the number of significant bits in the mask. A host name
# that starts with a dot (.) matches a suffix of the actual host name.
# Alternatively, you can write an IP address and netmask in separate
# columns to specify the set of hosts. Instead of a CIDR-address, you
# can write "samehost" to match any of the server's own IP addresses,
# or "samenet" to match any address in any subnet that the server is
# directly connected to.
#
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
# "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
# Note that "password" sends passwords in clear text; "md5" or
# "scram-sha-256" are preferred since they send encrypted passwords.
#
# OPTIONS are a set of options for the authentication in the format
# NAME=VALUE. The available options depend on the different
# authentication methods -- refer to the "Client Authentication"
# section in the documentation for a list of which options are
# available for which authentication methods.
#
# Database and user names containing spaces, commas, quotes and other
# special characters must be quoted. Quoting one of the keywords
# "all", "sameuser", "samerole" or "replication" makes the name lose
# its special character, and just match a database or username with
# that name.
#
# This file is read on server startup and when the server receives a
# SIGHUP signal. If you edit the file on a running system, you have to
# SIGHUP the server for the changes to take effect, run "pg_ctl reload",
# or execute "SELECT pg_reload_conf()".
#
# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records. In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.




# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local all postgres peer

# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-25

由于是开发环境,设置为允许局域网内免密码连接,加入以下一行:

1
2
3
# TYPE  DATABASE        USER                  ADDRESS                 METHOD
# IPV4 所有数据库 多个用户“,”分割 所有地址 信任,不用密码连接
host all test,demo 0.0.0.0/0 trust

五、开启允许远程连接

默认是只允许本地 ‘localhost’ 连接,改为 ‘*’,重启支持远程连接。

1
2
3
4
5
6
7
8
9
$ sudo vim /etc/postgresql/14/main/postgresql.conf

#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 = '*'

$ service postgresql restart

用IDEA自带的数据库工具测试test账号无密码连接成功。

如果是要密码,则可以这样设置:

1
host    all             all             0.0.0.0/0               md5

这样的话,要用 用户名+密码(demo)来连接,没有设置密码的用户(test)则不能连接。


六、安装Web图形工具pgAdmin

参考

  • 【pgadmin download】
  • 【pgadmin apt)】
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ sudo curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
    $ sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

    # Install for both desktop and web modes:
    sudo apt install pgadmin4

    # Install for desktop mode only:
    sudo apt install pgadmin4-desktop

    # Install for web mode only:
    sudo apt install pgadmin4-web

    # Configure the webserver, if you installed pgadmin4-web:
    sudo /usr/pgadmin4/bin/setup-web.sh