Ubuntu PostgreSQL 설치

N'CHE·2021년 10월 18일
0

1. Ubuntu의 버전, 코드명 확인

$ lsb_release -r -c -s
18.04
bionic

2. Repository 추가

# Create the file repository configuration.
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

3. Repository key 가져오기

# Import the repository signing key.
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

4. Package 목록 업데이트

# Update the package lists.
$ sudo apt-get update

Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Hit:2 http://kr.archive.ubuntu.com/ubuntu bionic InRelease
Get:3 http://kr.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://kr.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt bionic-pgdg InRelease [110 kB]
Get:6 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 Packages [249 kB]
Get:7 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main i386 Packages [248 kB]
Fetched 859 kB in 5s (162 kB/s)
Reading package lists... Done

5. Package 설치

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql'

$ sudo apt-get -y install postgresql-13

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libpq-dev
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
  postgresql-client-13
Suggested packages:
  postgresql-doc-13
The following NEW packages will be installed:
  postgresql-13 postgresql-client-13
0 upgraded, 2 newly installed, 0 to remove and 41 not upgraded.
Need to get 16.5 MB of archives.
After this operation, 54.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-client-13 amd64 13.4-4.pgdg18.04+1 [1510 kB]
Get:2 http://apt.postgresql.org/pub/repos/apt bionic-pgdg/main amd64 postgresql-13 amd64 13.4-4.pgdg18.04+1 [14.9 MB]
Fetched 16.5 MB in 5s (3048 kB/s)
Preconfiguring packages ...
Selecting previously unselected package postgresql-client-13.
(Reading database ... 133934 files and directories currently installed.)
Preparing to unpack .../postgresql-client-13_13.4-4.pgdg18.04+1_amd64.deb ...
Unpacking postgresql-client-13 (13.4-4.pgdg18.04+1) ...
Selecting previously unselected package postgresql-13.
Preparing to unpack .../postgresql-13_13.4-4.pgdg18.04+1_amd64.deb ...
Unpacking postgresql-13 (13.4-4.pgdg18.04+1) ...
Setting up postgresql-client-13 (13.4-4.pgdg18.04+1) ...
Setting up postgresql-13 (13.4-4.pgdg18.04+1) ...
Error: The locale requested by the environment is invalid:
  LANG: en.US.UTF-8
Error: could not create default cluster. Please create it manually with

  pg_createcluster 13 main --start

or a similar command (see 'man pg_createcluster').
Processing triggers for postgresql-common (231.pgdg18.04+1) ...
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:

6. cluster 생성

$ sudo pg_createcluster 13 main --start

Creating new PostgreSQL cluster 13/main ...
/usr/lib/postgresql/13/bin/initdb -D /var/lib/postgresql/13/main --auth-local peer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/13/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Seoul
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctlcluster 13 main start

Ver Cluster Port Status Owner    Data directory              Log file
13  main    5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log

7. PostgreSQL 설치 확인

$ psql --version
psql (PostgreSQL) 14.0 (Ubuntu 14.0-1.pgdg18.04+1)

8. PostgreSQL SuperUser 설정

# Initialize password of postgres(superuser).
$ sudo -u postgres psql
postgres=# \password postgres
Enter new password:
Enter it again:
postgres=# \q

9. PostgreSQL 로그인

$ psql -h localhost -U postgres -d postgres
Password for user postgres:
psql (14.0 (Ubuntu 14.0-1.pgdg18.04+1), server 13.4 (Ubuntu 13.4-4.pgdg18.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

# List all the PostgreSQL users available
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Reference

0개의 댓글