$ 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
설치가 완료되면 사용법에 대해 알려주며, 설치 버전 확인을 통해 설치가 정상적으로 됐는지 확인한다.
$ 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명=>로 뜬다.
계정 조회 하기
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
host, username, password등 사용할 계정 정보를 입력하여 시작할 수 있다.
다음과 같이 show all databases를 설정해주어야지 기존에 생성한 모든 database가 목록에 보인다.