์ฐธ๊ณ ํญ๋ชฉ
GitHub ํธ์คํธ ์คํ๊ธฐ๋ ํ์ฌ GitHub Enterprise Server์์ ์ง์๋์ง ์์ต๋๋ค.
์๊ฐ
์ด ๊ฐ์ด๋์์๋ Docker Hub postgres
์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ์ฌ ์๋น์ค ์ปจํ
์ด๋๋ฅผ ๊ตฌ์ฑํ๋ ์ํฌํ๋ก ์์ ๋ฅผ ๋ณด์ฌ ์ค๋๋ค. ์ํฌํ๋ก๋ PostgreSQL ์๋น์ค์ ์ฐ๊ฒฐํ๊ณ ํ
์ด๋ธ์ ๋ง๋ ๋ค์ ๋ฐ์ดํฐ๋ก ์ฑ์ฐ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํฉ๋๋ค. ์ํฌํ๋ก๊ฐ PostgreSQL ํ
์ด๋ธ์ ๋ง๋ค๊ณ ์ฑ์ฐ๋์ง ํ
์คํธํ๊ธฐ ์ํด ์คํฌ๋ฆฝํธ๋ ํ
์ด๋ธ์์ ์ฝ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ธ์ํฉ๋๋ค.
์ฐธ๊ณ ํญ๋ชฉ
์ํฌํ๋ก์์ Docker ์ปจํ ์ด๋ ์์ , ์์ ์ปจํ ์ด๋, ์๋น์ค ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, Linux ์คํ๊ธฐ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- GitHub์์ ํธ์คํธ๋๋ ์คํ๊ธฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ Ubuntu ์คํ๊ธฐ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
- ์์ฒด ํธ์คํ ์คํ๊ธฐ๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์คํ๊ธฐ๋ก Linux ์ปดํจํฐ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ฉฐ Docker๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค.
ํ์ ์กฐ๊ฑด
์๋น์ค ์ปจํ ์ด๋๊ฐ GitHub Actions์์ ์๋ํ๋ ๋ฐฉ์๊ณผ ์คํ๊ธฐ ๋๋ ์ปจํ ์ด๋์์ ์ง์ ์คํ ์ค์ธ ์์ ๊ฐ์ ๋คํธ์ํน ์ฐจ์ด์ ์ ๋ํด ์ ์๊ณ ์์ด์ผ ํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ Docker ์๋น์ค ์ปจํ ์ด๋์ ํต์ ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
YAML, GitHub Actions์ ๊ตฌ๋ฌธ ๋ฐ PostgreSQL์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ์ดํด๊ฐ ๋์์ด ๋ ์๋ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ๋ค์์ ์ฐธ์กฐํ์ธ์.
- ์ํฌํ๋ก ์์ฑ
- PostgreSQL ์ค๋ช ์์ PostgreSQL ์์ต์
์ปจํ ์ด๋์์ ์์ ์คํ
์ปจํ ์ด๋์์ ์คํ๋๋๋ก ์์ ์ ๊ตฌ์ฑํ๋ฉด ์์ ๊ณผ ์๋น์ค ์ปจํ ์ด๋ ๊ฐ์ ๋คํธ์ํน ๊ตฌ์ฑ์ด ๊ฐ์ํ๋ฉ๋๋ค. ๋์ผํ ์ฌ์ฉ์ ์ ์ ๋ธ๋ฆฌ์ง ๋คํธ์ํฌ์ Docker ์ปจํ ์ด๋๋ ๋ชจ๋ ํฌํธ๋ฅผ ์๋ก ๋ ธ์ถํ๋ฏ๋ก ์๋น์ค ์ปจํ ์ด๋ ํฌํธ๋ฅผ Docker ํธ์คํธ์ ๋งคํํ ํ์๊ฐ ์์ต๋๋ค. ์ํฌํ๋ก์์ ๊ตฌ์ฑํ๋ ๋ ์ด๋ธ์ ์ฌ์ฉํ์ฌ ์์ ์ปจํ ์ด๋์์ ์๋น์ค ์ปจํ ์ด๋์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์ด ์ํฌํ๋ก ํ์ผ์ ๋ฆฌํฌ์งํ ๋ฆฌ์ .github/workflows
๋๋ ํฐ๋ฆฌ์ ๋ณต์ฌํ๊ณ ํ์์ ๋ฐ๋ผ ์์ ํ ์ ์์ต๋๋ค.
name: PostgreSQL service example on: push jobs: # Label of the container job container-job: # Containers must run in Linux based operating systems runs-on: ubuntu-latest # Docker Hub image that `container-job` executes in container: node:20-bookworm-slim # Service containers to run with `container-job` services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to PostgreSQL # Runs a script that creates a PostgreSQL table, populates # the table with data, and then retrieves the data. run: node client.js # Environment variables used by the `client.js` script to create a new PostgreSQL table. env: # The hostname used to communicate with the PostgreSQL service container POSTGRES_HOST: postgres # The default PostgreSQL port POSTGRES_PORT: 5432
name: PostgreSQL service example
on: push
jobs:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: node:20-bookworm-slim
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see https://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to PostgreSQL
# Runs a script that creates a PostgreSQL table, populates
# the table with data, and then retrieves the data.
run: node client.js
# Environment variables used by the `client.js` script to create a new PostgreSQL table.
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: postgres
# The default PostgreSQL port
POSTGRES_PORT: 5432
์ปจํ ์ด๋์ ์์ ์ ๋ํ ์คํ๊ธฐ ์์ ๊ตฌ์ฑ
์ด ์ํฌํ๋ก๋ node:20-bookworm-slim
์ปจํ
์ด๋์์ ์คํ๋๋ ์์
์ ๊ตฌ์ฑํ๊ณ ubuntu-latest
์คํ๊ธฐ๋ฅผ ์ปจํ
์ด๋์ Docker ํธ์คํธ๋ก ์ฌ์ฉํฉ๋๋ค. node:20-bookworm-slim
์ปจํ
์ด๋์ ๋ํ ์์ธํ ๋ด์ฉ์ Docker Hub์ ๋
ธ๋ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
์ํฌํ๋ก๋ postgres
๋ ์ด๋ธ์ ์ฌ์ฉํ์ฌ ์๋น์ค ์ปจํ
์ด๋๋ฅผ ๊ตฌ์ฑํฉ๋๋ค. ๋ชจ๋ ์๋น์ค๋ ์ปจํ
์ด๋์์ ์คํ๋์ด์ผ ํ๋ฏ๋ก ๊ฐ ์๋น์ค๋ ์ปจํ
์ด๋ image
๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค. ์ด ์์ ์์๋ postgres
์ปจํ
์ด๋ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๊ณ , ๊ธฐ๋ณธ PostgreSQL ์ํธ๋ฅผ ์ ๊ณตํ๋ฉฐ, ์๋น์ค๊ฐ ์คํ ์ค์ธ์ง ํ์ธํ๊ธฐ ์ํ ์ํ ๊ฒ์ฌ ์ต์
์ ํฌํจํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ Docker Hub์ postgres ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
jobs: # Label of the container job container-job: # Containers must run in Linux based operating systems runs-on: ubuntu-latest # Docker Hub image that `container-job` executes in container: node:20-bookworm-slim # Service containers to run with `container-job` services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
jobs:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: node:20-bookworm-slim
# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
์ปจํ ์ด๋์์ ์์ ์ ๋ํ ๋จ๊ณ ๊ตฌ์ฑ
์ํฌํ๋ก๋ ๋ค์ ๋จ๊ณ๋ฅผ ์ํํฉ๋๋ค.
- ์คํ๊ธฐ์์ ๋ฆฌํฌ์งํ ๋ฆฌ ์ฒดํฌ ์์
- ์ข ์์ฑ ์ค์น
- ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ฌ ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to PostgreSQL # Runs a script that creates a PostgreSQL table, populates # the table with data, and then retrieves the data. run: node client.js # Environment variable used by the `client.js` script to create # a new PostgreSQL client. env: # The hostname used to communicate with the PostgreSQL service container POSTGRES_HOST: postgres # The default PostgreSQL port POSTGRES_PORT: 5432
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see https://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to PostgreSQL
# Runs a script that creates a PostgreSQL table, populates
# the table with data, and then retrieves the data.
run: node client.js
# Environment variable used by the `client.js` script to create
# a new PostgreSQL client.
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: postgres
# The default PostgreSQL port
POSTGRES_PORT: 5432
client.js ์คํฌ๋ฆฝํธ๋ ํด๋ผ์ด์ธํธ๋ฅผ ๋ง๋ค POSTGRES_HOST
ํ๊ฒฝ ๋ณ์ ๋ฐ POSTGRES_PORT
ํ๊ฒฝ ๋ณ์๋ฅผ ์ฐพ์ต๋๋ค. ์ํฌํ๋ก๋ ๋ ํ๊ฒฝ ๋ณ์๋ฅผ "PostgreSQL์ ์ปค๋ฅํธ" ๋จ๊ณ์ ์ผ๋ถ๋ก ์ค์ ํ์ฌ client.js ์คํฌ๋ฆฝํธ์ ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค. ์คํฌ๋ฆฝํธ์ ๋ํ ์์ธํ ๋ด์ฉ์ PostgreSQL ์๋น์ค ์ปจํ
์ด๋ ํ
์คํธ๋ฅผ ์ฐธ์กฐํ์ธ์.
PostgreSQL ์๋น์ค์ ํธ์คํธ ์ด๋ฆ์ ์ํฌํ๋ก์์ ๊ตฌ์ฑํ ๋ ์ด๋ธ(์ด ๊ฒฝ์ฐ postgres
)์
๋๋ค. ๋์ผํ ์ฌ์ฉ์ ์ ์ ๋ธ๋ฆฌ์ง ๋คํธ์ํฌ์ Docker ์ปจํ
์ด๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ํฌํธ๋ฅผ ์ด๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ PostgreSQL ํฌํธ 5432์ ์๋น์ค ์ปจํ
์ด๋์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์คํ๊ธฐ ์ปดํจํฐ์์ ์ง์ ์์ ์คํ
์คํ๊ธฐ ์ปดํจํฐ์์ ์ง์ ์์
์ ์คํํ๋ ๊ฒฝ์ฐ ์๋น์ค ์ปจํ
์ด๋์ ํฌํธ๋ฅผ Docker ํธ์คํธ์ ํฌํธ์ ๋งคํํด์ผ ํฉ๋๋ค. localhost
๋ฐ Docker ํธ์คํธ ํฌํธ ๋ฒํธ๋ฅผ ์ฌ์ฉํ์ฌ Docker ํธ์คํธ์์ ์๋น์ค ์ปจํ
์ด๋์ ์ก์ธ์คํ ์ ์์ต๋๋ค.
์ด ์ํฌํ๋ก ํ์ผ์ ๋ฆฌํฌ์งํ ๋ฆฌ์ .github/workflows
๋๋ ํฐ๋ฆฌ์ ๋ณต์ฌํ๊ณ ํ์์ ๋ฐ๋ผ ์์ ํ ์ ์์ต๋๋ค.
name: PostgreSQL Service Example on: push jobs: # Label of the runner job runner-job: # You must use a Linux environment when using service containers or container jobs runs-on: ubuntu-latest # Service containers to run with `runner-job` services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps tcp port 5432 on service container to the host - 5432:5432 steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to PostgreSQL # Runs a script that creates a PostgreSQL table, populates # the table with data, and then retrieves the data run: node client.js # Environment variables used by the `client.js` script to create # a new PostgreSQL table. env: # The hostname used to communicate with the PostgreSQL service container POSTGRES_HOST: localhost # The default PostgreSQL port POSTGRES_PORT: 5432
name: PostgreSQL Service Example
on: push
jobs:
# Label of the runner job
runner-job:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
# Service containers to run with `runner-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see https://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to PostgreSQL
# Runs a script that creates a PostgreSQL table, populates
# the table with data, and then retrieves the data
run: node client.js
# Environment variables used by the `client.js` script to create
# a new PostgreSQL table.
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: localhost
# The default PostgreSQL port
POSTGRES_PORT: 5432
์คํ๊ธฐ ์ปดํจํฐ์์ ์ง์ ์์ ์ ๋ํ ์คํ๊ธฐ ์์ ๊ตฌ์ฑ
์ด ์์ ์์๋ ubuntu-latest
์คํ๊ธฐ๋ฅผ Docker ํธ์คํธ๋ก ์ฌ์ฉํฉ๋๋ค.
์ํฌํ๋ก๋ postgres
๋ ์ด๋ธ์ ์ฌ์ฉํ์ฌ ์๋น์ค ์ปจํ
์ด๋๋ฅผ ๊ตฌ์ฑํฉ๋๋ค. ๋ชจ๋ ์๋น์ค๋ ์ปจํ
์ด๋์์ ์คํ๋์ด์ผ ํ๋ฏ๋ก ๊ฐ ์๋น์ค๋ ์ปจํ
์ด๋ image
๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค. ์ด ์์ ์์๋ postgres
์ปจํ
์ด๋ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๊ณ , ๊ธฐ๋ณธ PostgreSQL ์ํธ๋ฅผ ์ ๊ณตํ๋ฉฐ, ์๋น์ค๊ฐ ์คํ ์ค์ธ์ง ํ์ธํ๊ธฐ ์ํ ์ํ ๊ฒ์ฌ ์ต์
์ ํฌํจํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ Docker Hub์ postgres ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ์ธ์.
์ํฌํ๋ก๋ PostgreSQL ์๋น์ค ์ปจํ
์ด๋์ ํฌํธ 5432๋ฅผ Docker ํธ์คํธ์ ๋งคํํฉ๋๋ค. ports
ํค์๋์ ๋ํ ์์ธํ ๋ด์ฉ์ Docker ์๋น์ค ์ปจํ
์ด๋์ ํต์ ์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
jobs: # Label of the runner job runner-job: # You must use a Linux environment when using service containers or container jobs runs-on: ubuntu-latest # Service containers to run with `runner-job` services: # Label used to access the service container postgres: # Docker Hub image image: postgres # Provide the password for postgres env: POSTGRES_PASSWORD: postgres # Set health checks to wait until postgres has started options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 ports: # Maps tcp port 5432 on service container to the host - 5432:5432
jobs:
# Label of the runner job
runner-job:
# You must use a Linux environment when using service containers or container jobs
runs-on: ubuntu-latest
# Service containers to run with `runner-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
์คํ๊ธฐ ์ปดํจํฐ์์ ์ง์ ์์ ์ ๋ํ ๋จ๊ณ ๊ตฌ์ฑ
์ํฌํ๋ก๋ ๋ค์ ๋จ๊ณ๋ฅผ ์ํํฉ๋๋ค.
- ์คํ๊ธฐ์์ ๋ฆฌํฌ์งํ ๋ฆฌ ์ฒดํฌ ์์
- ์ข ์์ฑ ์ค์น
- ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ฌ ํด๋ผ์ด์ธํธ ๋ง๋ค๊ธฐ
steps: # Downloads a copy of the code in your repository before running CI tests - name: Check out repository code uses: actions/checkout@v4 # Performs a clean installation of all dependencies in the `package.json` file # For more information, see https://docs.npmjs.com/cli/ci.html - name: Install dependencies run: npm ci - name: Connect to PostgreSQL # Runs a script that creates a PostgreSQL table, populates # the table with data, and then retrieves the data run: node client.js # Environment variables used by the `client.js` script to create # a new PostgreSQL table. env: # The hostname used to communicate with the PostgreSQL service container POSTGRES_HOST: localhost # The default PostgreSQL port POSTGRES_PORT: 5432
steps:
# Downloads a copy of the code in your repository before running CI tests
- name: Check out repository code
uses: actions/checkout@v4
# Performs a clean installation of all dependencies in the `package.json` file
# For more information, see https://docs.npmjs.com/cli/ci.html
- name: Install dependencies
run: npm ci
- name: Connect to PostgreSQL
# Runs a script that creates a PostgreSQL table, populates
# the table with data, and then retrieves the data
run: node client.js
# Environment variables used by the `client.js` script to create
# a new PostgreSQL table.
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: localhost
# The default PostgreSQL port
POSTGRES_PORT: 5432
client.js ์คํฌ๋ฆฝํธ๋ ํด๋ผ์ด์ธํธ๋ฅผ ๋ง๋ค POSTGRES_HOST
ํ๊ฒฝ ๋ณ์ ๋ฐ POSTGRES_PORT
ํ๊ฒฝ ๋ณ์๋ฅผ ์ฐพ์ต๋๋ค. ์ํฌํ๋ก๋ ๋ ํ๊ฒฝ ๋ณ์๋ฅผ "PostgreSQL์ ์ปค๋ฅํธ" ๋จ๊ณ์ ์ผ๋ถ๋ก ์ค์ ํ์ฌ client.js ์คํฌ๋ฆฝํธ์ ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค. ์คํฌ๋ฆฝํธ์ ๋ํ ์์ธํ ๋ด์ฉ์ PostgreSQL ์๋น์ค ์ปจํ
์ด๋ ํ
์คํธ๋ฅผ ์ฐธ์กฐํ์ธ์.
ํธ์คํธ ์ด๋ฆ์ localhost
๋๋ 127.0.0.1
์
๋๋ค.
PostgreSQL ์๋น์ค ์ปจํ ์ด๋ ํ ์คํธ
PostgreSQL ์๋น์ค์ ์ฐ๊ฒฐํ๊ณ ์ผ๋ถ ์๋ฆฌ ํ์์ ๋ฐ์ดํฐ๊ฐ ์๋ ์ ํ
์ด๋ธ์ ์ถ๊ฐํ๋ ๋ค์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ์ํฌํ๋ก๋ฅผ ํ
์คํธํ ์ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์, ์คํฌ๋ฆฝํธ๋ PostgreSQL ํ
์ด๋ธ์ ์ ์ฅ๋ ๊ฐ์ ํฐ๋ฏธ๋์ ์ถ๋ ฅํฉ๋๋ค. ์คํฌ๋ฆฝํธ๋ ์ํ๋ ์ธ์ด๋ฅผ ์ฌ์ฉํ ์ ์์ง๋ง ์ด ์์ ์์๋ Node.js ๋ฐ pg
npm ๋ชจ๋์ ์ฌ์ฉํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ npm pg ๋ชจ๋์ ์ฐธ์กฐํ์ธ์.
์ํฌํ๋ก์ ํ์ํ PostgreSQL ์์
์ ํฌํจํ๋๋ก _client.js_๋ฅผ ์์ ํ ์ ์์ต๋๋ค. ์ด ์์ ์์ ์คํฌ๋ฆฝํธ๋ PostgreSQL ์๋น์ค์ ์ฐ๊ฒฐํ๊ณ , postgres
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ
์ด๋ธ์ ์ถ๊ฐํ๊ณ , ์ผ๋ถ ์๋ฆฌ ํ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ ๋ค์, ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํฉ๋๋ค.
๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ _client.js_๋ผ๋ ์ ํ์ผ์ ๋ฆฌํฌ์งํ ๋ฆฌ์ ์ถ๊ฐํฉ๋๋ค.
const { Client } = require('pg'); const pgclient = new Client({ host: process.env.POSTGRES_HOST, port: process.env.POSTGRES_PORT, user: 'postgres', password: 'postgres', database: 'postgres' }); pgclient.connect(); const table = 'CREATE TABLE student(id SERIAL PRIMARY KEY, firstName VARCHAR(40) NOT NULL, lastName VARCHAR(40) NOT NULL, age INT, address VARCHAR(80), email VARCHAR(40))' const text = 'INSERT INTO student(firstname, lastname, age, address, email) VALUES($1, $2, $3, $4, $5) RETURNING *' const values = ['Mona the', 'Octocat', 9, '88 Colin P Kelly Jr St, San Francisco, CA 94107, United States', 'octocat@github.com'] pgclient.query(table, (err, res) => { if (err) throw err }); pgclient.query(text, values, (err, res) => { if (err) throw err }); pgclient.query('SELECT * FROM student', (err, res) => { if (err) throw err console.log(err, res.rows) // Print the data in student table pgclient.end() });
const { Client } = require('pg');
const pgclient = new Client({
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
user: 'postgres',
password: 'postgres',
database: 'postgres'
});
pgclient.connect();
const table = 'CREATE TABLE student(id SERIAL PRIMARY KEY, firstName VARCHAR(40) NOT NULL, lastName VARCHAR(40) NOT NULL, age INT, address VARCHAR(80), email VARCHAR(40))'
const text = 'INSERT INTO student(firstname, lastname, age, address, email) VALUES($1, $2, $3, $4, $5) RETURNING *'
const values = ['Mona the', 'Octocat', 9, '88 Colin P Kelly Jr St, San Francisco, CA 94107, United States', 'octocat@github.com']
pgclient.query(table, (err, res) => {
if (err) throw err
});
pgclient.query(text, values, (err, res) => {
if (err) throw err
});
pgclient.query('SELECT * FROM student', (err, res) => {
if (err) throw err
console.log(err, res.rows) // Print the data in student table
pgclient.end()
});
์ด ์คํฌ๋ฆฝํธ๋ PostgreSQL ์๋น์ค์ ๋ํ ์ ์ฐ๊ฒฐ์ ๋ง๋ค๊ณ POSTGRES_HOST
๋ฐ POSTGRES_PORT
ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ PostgreSQL ์๋น์ค IP ์ฃผ์ ๋ฐ ํฌํธ๋ฅผ ์ง์ ํฉ๋๋ค. host
๋ฐ port
๊ฐ ์ ์๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ํธ์คํธ๋ localhost
์ด๊ณ ๊ธฐ๋ณธ ํฌํธ๋ 5432์
๋๋ค.
์คํฌ๋ฆฝํธ๋ ํ
์ด๋ธ์ ๋ง๋ค๊ณ ์๋ฆฌ ํ์์ ๋ฐ์ดํฐ๋ก ํ
์ด๋ธ์ ์ฑ์๋๋ค. postgres
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ๊ฐ ํฌํจ๋์ด ์๋์ง ํ
์คํธํ๊ธฐ ์ํด ์คํฌ๋ฆฝํธ๋ ํ
์ด๋ธ์ ๋ด์ฉ์ ์ฝ์ ๋ก๊ทธ์ ์ถ๋ ฅํฉ๋๋ค.
์ด ์ํฌํ๋ก๋ฅผ ์คํํ๋ฉด PostgreSQL ํ ์ด๋ธ์ ์ฑ๊ณต์ ์ผ๋ก ๋ง๋ค๊ณ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋์ง ํ์ธํ๋ โPostgreSQL์ ์ฐ๊ฒฐโ ๋จ๊ณ์์ ๋ค์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
null [ { id: 1,
firstname: 'Mona the',
lastname: 'Octocat',
age: 9,
address:
'88 Colin P Kelly Jr St, San Francisco, CA 94107, United States',
email: 'octocat@github.com' } ]