Skip to main content

PostgreSQL ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ ๋งŒ๋“ค๊ธฐ

์›Œํฌํ”Œ๋กœ์—์„œ ์‚ฌ์šฉํ•  PostgreSQL ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ ๋˜๋Š” ์‹คํ–‰๊ธฐ ์ปดํ“จํ„ฐ์—์„œ ์ง์ ‘ ์‹คํ–‰๋˜๋Š” ์ž‘์—…์— ๋Œ€ํ•œ PostgreSQL ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ ์ค๋‹ˆ๋‹ค.

์ฐธ๊ณ  ํ•ญ๋ชฉ

GitHub ํ˜ธ์ŠคํŠธ ์‹คํ–‰๊ธฐ๋Š” ํ˜„์žฌ GitHub Enterprise Server์—์„œ ์ง€์›๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์†Œ๊ฐœ

์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” Docker Hub postgres ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์›Œํฌํ”Œ๋กœ ์˜ˆ์ œ๋ฅผ ๋ณด์—ฌ ์ค๋‹ˆ๋‹ค. ์›Œํฌํ”Œ๋กœ๋Š” PostgreSQL ์„œ๋น„์Šค์— ์—ฐ๊ฒฐํ•˜๊ณ  ํ…Œ์ด๋ธ”์„ ๋งŒ๋“  ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋กœ ์ฑ„์šฐ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์›Œํฌํ”Œ๋กœ๊ฐ€ PostgreSQL ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๊ณ  ์ฑ„์šฐ๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ์Šคํฌ๋ฆฝํŠธ๋Š” ํ…Œ์ด๋ธ”์—์„œ ์ฝ˜์†”๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ๊ณ  ํ•ญ๋ชฉ

์›Œํฌํ”Œ๋กœ์—์„œ Docker ์ปจํ…Œ์ด๋„ˆ ์ž‘์—…, ์ž‘์—… ์ปจํ…Œ์ด๋„ˆ, ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, Linux ์‹คํ–‰๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  • GitHub์—์„œ ํ˜ธ์ŠคํŠธ๋˜๋Š” ์‹คํ–‰๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ Ubuntu ์‹คํ–‰๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ž์ฒด ํ˜ธ์ŠคํŒ… ์‹คํ–‰๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์‹คํ–‰๊ธฐ๋กœ Linux ์ปดํ“จํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ Docker๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ•„์ˆ˜ ์กฐ๊ฑด

์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ GitHub Actions์—์„œ ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์‹คํ–‰๊ธฐ ๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ง์ ‘ ์‹คํ–‰ ์ค‘์ธ ์ž‘์—… ๊ฐ„์˜ ๋„คํŠธ์›Œํ‚น ์ฐจ์ด์ ์— ๋Œ€ํ•ด ์ž˜ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Docker ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ์™€ ํ†ต์‹ ์„(๋ฅผ) ์ฐธ์กฐํ•˜์„ธ์š”.

YAML, GitHub Actions์˜ ๊ตฌ๋ฌธ ๋ฐ PostgreSQL์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ดํ•ด๊ฐ€ ๋„์›€์ด ๋  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

์ปจํ…Œ์ด๋„ˆ์—์„œ ์ž‘์—… ์‹คํ–‰

์ปจํ…Œ์ด๋„ˆ์—์„œ ์‹คํ–‰๋˜๋„๋ก ์ž‘์—…์„ ๊ตฌ์„ฑํ•˜๋ฉด ์ž‘์—…๊ณผ ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ๋„คํŠธ์›Œํ‚น ๊ตฌ์„ฑ์ด ๊ฐ„์†Œํ™”๋ฉ๋‹ˆ๋‹ค. ๋™์ผํ•œ ์‚ฌ์šฉ์ž ์ •์˜ ๋ธŒ๋ฆฌ์ง€ ๋„คํŠธ์›Œํฌ์˜ Docker ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ชจ๋“  ํฌํŠธ๋ฅผ ์„œ๋กœ ๋…ธ์ถœํ•˜๋ฏ€๋กœ ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ๋ฅผ Docker ํ˜ธ์ŠคํŠธ์— ๋งคํ•‘ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์›Œํฌํ”Œ๋กœ์—์„œ ๊ตฌ์„ฑํ•˜๋Š” ๋ ˆ์ด๋ธ”์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์—… ์ปจํ…Œ์ด๋„ˆ์—์„œ ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์›Œํฌํ”Œ๋กœ ํŒŒ์ผ์„ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ .github/workflows ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋ณต์‚ฌํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

YAML
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 ์ด๋ฏธ์ง€๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

YAML
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

์ปจํ…Œ์ด๋„ˆ์—์„œ ์ž‘์—…์— ๋Œ€ํ•œ ๋‹จ๊ณ„ ๊ตฌ์„ฑ

์›Œํฌํ”Œ๋กœ๋Š” ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  1. ์‹คํ–‰๊ธฐ์—์„œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ฒดํฌ ์•„์›ƒ
  2. ์ข…์†์„ฑ ์„ค์น˜
  3. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ๋งŒ๋“ค๊ธฐ
YAML
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 ๋””๋ ‰ํ„ฐ๋ฆฌ์— ๋ณต์‚ฌํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

YAML
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 ์„œ๋น„์Šค ์ปจํ…Œ์ด๋„ˆ์™€ ํ†ต์‹ ์„(๋ฅผ) ์ฐธ์กฐํ•˜์„ธ์š”.

YAML
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

์‹คํ–‰๊ธฐ ์ปดํ“จํ„ฐ์—์„œ ์ง์ ‘ ์ž‘์—…์— ๋Œ€ํ•œ ๋‹จ๊ณ„ ๊ตฌ์„ฑ

์›Œํฌํ”Œ๋กœ๋Š” ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  1. ์‹คํ–‰๊ธฐ์—์„œ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ฒดํฌ ์•„์›ƒ
  2. ์ข…์†์„ฑ ์„ค์น˜
  3. ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ๋งŒ๋“ค๊ธฐ
YAML
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_๋ผ๋Š” ์ƒˆ ํŒŒ์ผ์„ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

JavaScript
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' } ]