PostgreSQL

Mac OS ์„ค์น˜ํ•˜๊ธฐ

์„ค์น˜์ „์— homebrew๋ฅผ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ updateํ•ด์ค€๋‹ค.

$ brew update

update๋ฅผ ์™„๋ฃŒํ•˜๋ฉด, ์„ค์น˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์ƒ‰ํ•ด๋ณธ๋‹ค.

$ brew search postgresql
==> Formulae
postgresql โœ”    postgresql@11   postgresql@13   postgresql@9.5  qt-postgresql
postgresql@10   postgresql@12   postgresql@9.4  postgresql@9.6  postgrest

์›ํ•˜๋Š” ๋ฒ„์ „์˜ postgresql์„ ์„ค์น˜ํ•ด์ค€๋‹ค.

$ brew install postgresql
...
==> Installing postgresql
==> Pouring postgresql--14.1_1.arm64_monterey.bottle.tar.gz
==> /opt/homebrew/Cellar/postgresql/14.1_1/bin/initdb --locale=C -E UTF-8 /opt/h
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
  brew services restart postgresql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres
==> Summary
๐Ÿบ  /opt/homebrew/Cellar/postgresql/14.1_1: 3,304 files, 44.5MB
==> Running `brew cleanup postgresql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
  brew services restart postgresql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres

์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ๋ ค์ฃผ๋ฉฐ, ์„ค์น˜ ๋ฒ„์ „ ํ™•์ธ์„ ํ†ตํ•ด ์„ค์น˜๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

$ postgres -V
postgres (PostgreSQL) 14.1

ํ˜„์žฌ ๊ธฐ์ค€ default ๋ฒ„์ „ 14.1์ด ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 1616, done.
remote: Counting objects: 100% (495/495), done.
remote: Compressing objects: 100% (361/361), done.
remote: Total 1616 (delta 208), reused 337 (delta 122), pack-reused 1121
Receiving objects: 100% (1616/1616), 470.41 KiB | 6.92 MiB/s, done.
Resolving deltas: 100% (684/684), done.
Tapped 1 command (42 files, 599.2KB).
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

start๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด postgresql์„ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ

์ ‘์†ํ•˜๊ธฐ

$ psql postgres
psql (14.1)
Type "help" for help.

postgres=#

superuser๋กœ ์ ‘์†ํ•˜๋Š” ๊ฒฝ์šฐ database๋ช…#๋กœ ๋œฌ๋‹ค.

ํŠน์ • ์‚ฌ์šฉ์ž๋กœ ํŠน์ • database ์ ‘์†ํ•˜๊ธฐ

$ psql spring -U test
psql (14.1)
Type "help" for help.

spring=>

๋‹ค์Œ๊ณผ ๊ฐ™์ด spring์œผ๋กœ ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. superuser๊ฐ€ ์•„๋‹Œ๊ฒฝ์šฐ์—๋Š” database๋ช…=>๋กœ ๋œฌ๋‹ค.

๊ณ„์ • ์กฐํšŒ ํ•˜๊ธฐ

https://www.postgresql.org/docs/current/postgres-user.html์— ๋ณด๋ฉด ์„ค์น˜์‹œ ์ž๋™์œผ๋กœ ์ ํ•ฉํ•œ ์‚ฌ์šฉ์ž ๊ณ„์ •์„ ์ƒ์„ฑํ•ด์ค€๋‹ค๊ณ  ๋˜์–ด์žˆ๋‹ค.

19.1. The PostgreSQL User Account
As with any server daemon that is accessible to the outside world, it is advisable to run PostgreSQL under a separate user account. This user account should only own the data that is managed by the server, and should not be shared with other daemons. (For example, using the user nobody is a bad idea.) In particular, it is advisable that this user account not own the PostgreSQL executable files, to ensure that a compromised server process could not modify those executables.

Pre-packaged versions of PostgreSQL will typically create a suitable user account automatically during package installation.

To add a Unix user account to your system, look for a command useradd or adduser. The user name postgres is often used, and is assumed throughout this book, but you can use another name if you like.
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 dahyelele | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

์กฐํšŒ ๋ช…๋ น์–ด๋ฅผ ์น˜๋ฉด, ๋ณ„๋„๋กœ ์ƒ์„ฑํ•œ์  ์—†๋Š” dahyelele(mac ๊ณ„์ •๋ช…) superuser๊ฐ€ ์ƒ์„ฑ๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ณ„์ • ์ƒ์„ฑํ•˜๊ธฐ

postgres=# CREATE ROLE test WITH LOGIN PASSWORD 'test!';

๊ถŒํ•œ ๋ถ€์—ฌํ•˜๊ธฐ

postgres=# ALTER ROLE test CREATEDB;

์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ๊ถŒํ•œ์„ ์ค„์ง€ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค. ๊ถŒํ•œ ๋ถ€์—ฌ ํ›„ \du๋กœ ๋ถ€์—ฌ๋œ ๊ถŒํ•œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 dahyelele | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 test      | No inheritance, Create DB                                  | {}

DB ๊ถŒํ•œ ๋ถ€์—ฌํ•˜๊ธฐ

postgres=> GRANT ALL PRIVILEGES ON DATABASE spring TO test;
GRANT

Database ๋ฆฌ์ŠคํŠธ ์กฐํšŒ

postgres=> \list
                              List of databases
   Name    |   Owner   | Encoding | Collate | Ctype |    Access privileges
-----------+-----------+----------+---------+-------+-------------------------
 postgres  | dahyelele | UTF8     | C       | C     |
 spring    | dahyelele | UTF8     | C       | C     | =Tc/dahyelele          +
           |           |          |         |       | dahyelele=CTc/dahyelele+
           |           |          |         |       | test=CTc/dahyelele
 template0 | dahyelele | UTF8     | C       | C     | =c/dahyelele           +
           |           |          |         |       | dahyelele=CTc/dahyelele
 template1 | dahyelele | UTF8     | C       | C     | =c/dahyelele           +
           |           |          |         |       | dahyelele=CTc/dahyelele
(4 rows)

Database ์—ฐ๊ฒฐํ•˜๊ธฐ

postgres=> \connect test

Table ๋ฆฌ์ŠคํŠธ ์กฐํšŒ

postgres=> \dt

Schema ์ƒ์„ฑ

PostgreSQL์—์„œ๋Š” Database -> Schema -> Table ๊ฐœ๋…์ด๋‹ค. ๊ทธ๋ž˜์„œ Schema๋ฅผ ์ƒ์„ฑํ•ด์ค€ ํ›„ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ์ด๋•Œ ๊ถŒํ•œ์ด ์žˆ๋Š” database๋กœ ์—ฐ๊ฒฐํ•œ ํ›„ ์Šคํ‚ค๋งˆ๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค.

spring=> CREATE SCHEMA spring;

Schema ์กฐํšŒ

spring=> \dn
  List of schemas
  Name  |   Owner
--------+-----------
 public | dahyelele
 spring | test

์Šคํ‚ค๋งˆ ๋ชฉ๋ก์„ ๋ณด๋ฉด test์— ์œ„์—์„œ ์ƒ์„ฑํ•œ spring์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

client

DBeaver๋Š” mysql, postgresql ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ database์ ‘๊ทผ์„ ์ œ๊ณตํ•ด์ค€๋‹ค.

host, username, password๋“ฑ ์‚ฌ์šฉํ•  ๊ณ„์ • ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด show all databases๋ฅผ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ์ง€ ๊ธฐ์กด์— ์ƒ์„ฑํ•œ ๋ชจ๋“  database๊ฐ€ ๋ชฉ๋ก์— ๋ณด์ธ๋‹ค.

์ฐธ๊ณ 

Last updated