Showing posts with label postgresql. Show all posts
Showing posts with label postgresql. Show all posts

Friday, November 3, 2023

postgreSQL의 pg_hba.conf 파일 설정

 pg_hba.conf 파일은 PostgreSQL에서 클라이언트 인증 설정을 저장하는 파일입니다. 이 파일의 각 줄은 특정 유형의 연결 요청에 대해 어떤 인증 방법을 사용할지 지정합니다.

당신이 제공한 pg_hba.conf의 내용을 한글로 설명하면 다음과 같습니다:

  1. Database administrative login by Unix domain socket

    • 유닉스 도메인 소켓을 통한 데이터베이스 관리자 로그인
    sql
    local all postgres peer
    • local은 유닉스 도메인 소켓 연결을 의미합니다.
    • all은 모든 데이터베이스를 의미합니다.
    • postgrespostgres 사용자를 의미합니다.
    • peer 인증은 운영체제 사용자와 PostgreSQL 사용자의 이름이 동일하다고 가정하고 인증합니다.
  2. "local" is for Unix domain socket connections only

    sql
    local all all peer
    • 모든 데이터베이스, 모든 사용자에 대한 유닉스 도메인 소켓 연결은 peer 인증 방식을 사용합니다.
  3. IPv4 local connections:

    css
    host all all 0.0.0.0/0 md5 host all all 127.0.0.1/32 trust
    • 첫 번째 줄은 모든 IPv4 주소에서의 연결에 대해 md5 인증 방식을 사용합니다.
    • 두 번째 줄은 로컬호스트(127.0.0.1)에서의 연결에 대해 인증 없이 접근을 허용합니다(trust).
  4. IPv6 local connections:

    css
    host all all ::1/128 scram-sha-256
    • IPv6 로컬호스트(::1)에서의 연결에 대해 scram-sha-256 인증 방식을 사용합니다.
  5. Allow replication connections from localhost, by a user with the replication privilege.

    sql
    local replication all peer host replication all 127.0.0.1/32 scram-sha-256 host replication all ::1/128 scram-sha-256
    • 복제 권한을 가진 사용자가 로컬호스트에서 복제 연결을 수행할 때의 인증 설정입니다.
  6. Other Connections:

    css
    host all all 172.26.0.0/16 trust
    • 172.26.0.0/16 IP 범위에서의 모든 연결에 대해 인증 없이 접근을 허용합니다(trust).

이 설정은 데이터베이스의 보안 수준과 연결 허용 범위를 결정하는 중요한 역할

Friday, October 27, 2023

Postgresql이 갑자기 안될 때

 

Postgresql이 갑자기 안될 때

아래와 같은 에러를 뿜으며 postgresql이 죽는다. postgresql의 로그는 우분투 리눅스의 경우 /var/log/postgresql/postgresql-버전번호-main.log 에서 볼 수 있다.

2019-00-00 00:00:00 KST [1469-2] LOG: received fast shutdown request
2019-00-00 00:00:00 KST [1469-3] LOG: aborting any active transactions
2019-00-00 00:00:00 KST [1481-2] LOG: autovacuum launcher shutting down
2019-00-00 00:00:00 KST [1478-1] LOG: shutting down
2019-00-00 00:00:00 KST [1478-2] LOG: database system is shut down
2019-00-00 00:00:00 KST [27487-1] FATAL: could not map anonymous shared memory: 메모리를 할당할 수 없습니다
2019-00-00 00:00:00 KST [27487-2] HINT: This error usually means that PostgreSQL’s request for a shared memory segment exceeded available memory, swap space, or huge pages. To reduce the request size (currently 148488192 bytes), reduce PostgreSQL’s shared memory usage, perhaps by reducing shared_buffers or max_connections.
2019-00-00 00:00:00 KST [1782-1] LOG: database system was shut down at 2019-00-00 00:00:00 KST


원인

아래 에러 메시지에 원인과 해결책이 있다.

2019-00-00 00:00:00 KST [27487-2] HINT: This error usually means that PostgreSQL’s request for a shared memory segment exceeded available memory, swap space, or huge pages. To reduce the request size (currently 148488192 bytes), reduce PostgreSQL’s shared memory usage, perhaps by reducing shared_buffers or max_connections.

메모리가 부족해서 서비스가 죽었다는 말이다.


해결방법

  1. free -m 명령으로 현재 메모리 상태를 본다. (뒤에 -m 옵션은 메가바이트 단위로 보겠다는 뜻이다.)

  2. /etc/postgresql/버전번호/main/postgresql.conf 설정 파일에서 약 113번 째 줄부터 메모리 관련 설정이 있다. 여기에서 shared_buffters = 128MB이런 식으로 되어 있는 값을 바꿔주면 된다. 만약 free -m으로 봤는데 buff/cache메모리가 110 이런 식으로 써져있으면 이 값보다 좀 넉넉하게 작게 설정해준다. (물론 자신이 운영하는 서비스 특성에 따라 다를 수 있다.) 나의 경우 buff/cache가 103이었고, postgresql 설정에서 shared_buffers 값을 60MB로 바꿔주고 postgresql 서비스를 재시작해주었다.


AWS 라이트셰일 $3.5/m 서비스의 메모리가 512MB인데 이렇게 메모리 오류가 나는 경우는 저사양의 서버에 기본값으로 서비스를 올려서 그런 경우가 종종 있으니 참고.