PostgreSQL FATAL: role does not exist 해결
PostgreSQL role "root" does not exist 는 -U 없이 OS 사용자 이름을 역할로 써서입니다. -U 로 있는 역할을 주고, Docker 는 POSTGRES_USER 를 봅니다.
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not existpsql 에 -U 를 주지 않으면 지금 OS 사용자 이름을 데이터베이스 역할 이름으로 씁니다. root 셸이나 ubuntu·ec2-user 같은 계정에서 그대로 psql 을 치면 그 이름의 역할이 없어 거절됩니다. -U postgres 처럼 있는 역할을 지정하거나, 그 이름으로 역할을 만들면 됩니다. Docker 에서 POSTGRES_USER 를 바꿨다면 postgres 역할 자체가 없습니다.
[1] 어떤 이름으로 붙었는지 봅니다. 오류 문구의 따옴표 안 이름이 서버가 받은 역할 이름입니다. 아래는 소켓 접속이 trust 인 Docker 공식 이미지에서 돌린 것이라 -U postgres 만으로 들어갔습니다. Debian·Ubuntu 패키지 설치본처럼 소켓이 peer 면 root 에서 -U postgres 는 Peer authentication failed 로 막히니, sudo -u postgres psql 로 들어갑니다(PostgreSQL Peer authentication failed 해결).
$ whoami; psqlrootpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist$ psql -U postgres -Atc 'SELECT current_user'postgresTCP(-h 127.0.0.1)로 붙어도 같은 이름 규칙이 적용됩니다. 비밀번호 인증을 쓰는 줄에 걸리면 클라이언트에는 password authentication failed 로 보이니 그 글의 서버 로그 확인을 함께 봅니다.
[2] 있는 역할 목록을 봅니다. 관리자 역할로 접속해 \du 를 칩니다.
$ psql -U postgres -c '\du' List of roles Role name | Attributes -----------+------------------------------------------------------------ appuser | postgres | Superuser, Create role, Create DB, Replication, Bypass RLS[3] 대소문자를 맞춥니다. SQL 에서 따옴표 없이 쓴 이름은 소문자로 바뀌지만, 접속할 때 주는 이름은 바뀌지 않습니다. CREATE ROLE AppUser 로 만든 역할의 실제 이름은 appuser 입니다.
$ psql -U postgres -c 'CREATE ROLE AppUser LOGIN'CREATE ROLE$ psql -U AppUser -d postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "AppUser" does not exist$ psql -U appuser -d postgres -Atc 'SELECT current_user'appuser애플리케이션 설정에 대문자가 섞인 사용자 이름이 있으면 소문자로 바꾸는 쪽이 쉽습니다. 대문자 이름이 꼭 필요하면 CREATE ROLE "AppUser" LOGIN 처럼 큰따옴표로 만들고, SQL 에서도 늘 따옴표로 씁니다.
[4] 역할을 만들거나 기본값을 정합니다. OS 사용자 이름으로 계속 쓰려면 같은 이름의 역할을 만듭니다.
$ createuser -U postgres root && psql -Atc 'SELECT current_user'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: database "root" does not exist$ psql -d postgres -Atc 'SELECT current_user'root역할은 생겼지만 이번엔 database "root" does not exist 입니다. 데이터베이스 이름도 기본값이 역할 이름이라서입니다 — PostgreSQL database does not exist 해결. 역할을 만들기 싫으면 환경 변수로 기본값만 바꿉니다.
$ PGUSER=postgres psql -Atc 'SELECT current_user'postgres셸 설정에 export PGUSER=postgres 를 두거나 ~/.pg_service.conf·연결 문자열에 사용자를 적습니다.
[5] 역할은 있는데 로그인 권한이 없으면 문구가 다릅니다. CREATE ROLE 은 기본이 NOLOGIN 이고, CREATE USER 만 LOGIN 이 기본입니다.
$ psql -U postgres -c 'CREATE ROLE reporter' && psql -U reporter -d postgresCREATE ROLEpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "reporter" is not permitted to log in$ psql -U postgres -c 'ALTER ROLE reporter LOGIN' && psql -U reporter -d postgres -Atc 'SELECT current_user'ALTER ROLEreporter권한을 묶어 두는 그룹 역할이면 LOGIN 을 주지 말고, 실제 접속용 역할을 따로 만들어 GRANT reporter TO <역할> 로 넣습니다.
[6] Docker 에서는 POSTGRES_USER 를 봅니다. 이 값을 주면 그 이름으로 관리자 역할이 만들어지고 postgres 역할은 생기지 않습니다.
$ docker exec db psql -U postgrespsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist$ docker exec db psql -U myapp -Atc 'SELECT current_user, current_database()'myapp|myapp값을 바꿔 컨테이너를 다시 만들었는데도 새 이름이 없다면, 데이터 볼륨이 남아 있어 초기화를 건너뛴 것입니다. 이미지 문서도 이 변수들은 빈 데이터 디렉터리로 시작할 때만 효과가 있다고 적습니다.
$ docker logs db 2>&1 | grep -i -E "skipping|initialization"PostgreSQL Database directory appears to contain a database; Skipping initialization$ docker exec db psql -U secondpsql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "second" does not exist$ docker exec db psql -U first -d first -Atc 'SELECT rolname FROM pg_roles WHERE rolcanlogin'first처음 만든 이름(first)으로 접속해 CREATE ROLE second LOGIN PASSWORD '<비밀번호>' 로 만들어 줍니다. 볼륨을 지우고 새로 시작하면 이름은 바뀌지만 데이터도 모두 사라집니다 — 시험용 컨테이너가 아니면 하지 않습니다.
공식 근거: psql — Connecting to a Database · CREATE ROLE · 식별자 대소문자(Lexical Structure) · libpq 환경 변수 PGUSER · Docker 공식 이미지 postgres — 환경 변수