DB

[postgreSQL] 데이터 베이스생성 및 연결하기(feat. 설치 중 오류 해결)

앳홍 2023. 8. 6. 15:47
반응형
데이터베이스 설치 및 생성
postgreSQL 설치할 수 있는 버전 확인 
brew search postgresql
==> Formulae
postgresql@10   postgresql@12   postgresql@14   postgresql@9.4  qt-postgresql
postgresql@11   postgresql@13   postgresql@15   postgresql@9.5  postgrest

# 위의 나오는 것들은 설치 가능한 postgresql 버전들을 나타낸다.

==> Casks
navicat-for-postgresql

If you meant "postgresql" specifically:
postgresql breaks existing databases on upgrade without human intervention.

See a more specific version to install with:
  brew formulae | grep postgresql@

 

postgreSQL 설치
brew install postgresql

 

여기부터 문제였다..

하..

버전 확인 
 postgres --version
dyld[2337]: Library not loaded: /opt/homebrew/opt/icu4c/lib/libicui18n.72.dylib
  Referenced from: /opt/homebrew/Cellar/postgresql@14/14.7/bin/postgres
  Reason: tried: '/opt/homebrew/opt/icu4c/lib/libicui18n.72.dylib' (no such file), '/usr/local/lib/libicui18n.72.dylib' (no such file), '/usr/lib/libicui18n.72.dylib' (no such file), '/opt/homebrew/Cellar/icu4c/73.2/lib/libicui18n.72.dylib' (no such file), '/usr/local/lib/libicui18n.72.dylib' (no such file), '/usr/lib/libicui18n.72.dylib' (no such file)
[1]    2337 abort      postgres --version

 

postgresSQL 시작
brew services start postgresql

 

postgresSQL에 접속
psql postgres
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
	Is the server running locally and accepting connections on that socket?

오류가 떠서 일단 종료 시도

brew services stop postgres

응안돼~ 오류~^^

Error: No available formula with the name "postgres". Did you mean postgrest or postgis?

 

어디서 잘못되었는지.. 길을 잃은 나는 삭제해버리기로 마음먹음

 


오류 해결 내용

brew 내부에 'sql' 이 포함되어있는 항목 확인

 

brew list | grep sql

 

 

확인했을때 'sqlite'와 내가 방금 설치한 'postgresql@14'가 출력되었다.

 

설치한 postgreSQL 관련 항목 깨끗하게 삭제
brew uninstall --force postgres
rm -rf /usr/local/var/postgres
rm -rf .psql_history .psqlrc .psql.local .pgpass .psqlrc.local
brew cleanup

 

postgreSQL 관련 항목들이 잘 삭제 되었는 지 확인
brew list | grep sql

그래도 postgresql@14 이 삭제되지 않고 표출되는 것으로 확인 되었다.

 

이건 uninstall 명령어를 통해 postgresql 설치한 걸 삭제하면 된다.

postgreSQL 삭제 및 확인
brew uninstall postgresql
brew list | grep sql

설치한 postgresql 를 삭제 한 후 다시 확인해보면 잘 삭제 된 것을 확인할 수 있을 것이다.

 

uninstall 만으로 설치한 항목이 삭제가 되지 않는 경우

위에 보여준 방법대로 설치한 postgreSQL 관련 항목을 깨끗하게 삭제 하면 된다.

 

그리고 나서 아래의 링크를 참고해서 13으로 다운그레이드를 하기로 결정했다.


 

참고) 13으로 다운그레이드 참고 페이지

 

psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory

Not really sure what caused this but most likely exiting the terminal while my rails server which was connected to PostgreSQL database was closed (not a good practice I know but lesson learned!) I've

stackoverflow.com

 

 

아래의 페이지는 오류 해결할때 참고하면 도움 될 것 같아서 첨부!

 

참고) brew 실행 오류 해결 관련 명령어

 

[homebrew] M1에 brew 실행 오류 해결 관련 명령어(doctor, link)

이 페이지에서의 모든 예시는 postgreSQL로 들겠다. brew doctor brew 명령어를 실행했을 때 오류가 발생한 경우 문제점 검사 brew doctor 예를 들어 postgresql 를 설치하기 위해서 brew install postgresql 명령어를

mingd0o0.tistory.com


postgresql@13으로 다운그레이드 설치
brew install postgresql@13
brew services start postgresql@13
brew link postgresql@13 --force	# 버전 강제로 연결하기

경로에 postgresql@13 가 있어야하는 경우 해당 환경 변수를 추가하라는 메세지이다.

echo 'export PATH="/opt/homebrew/opt/postgresql@13/bin:$PATH"' >> ~/.zshrc
# postgres 접속
psql postgres

환경변수 추가 후 psql postgres 를 실행했을때

결과 : 아래와같이 postgres =# 라고 나와야지 정상적으로 실행된것이다.

 

권한 잘 설정됐는지 확인
\du

처음에는 한개의 role name이 생성 되어있다.(모두 완료한 후 이미지를 따와서 두개로 나왔지만.. 하나라고 생각해주세오...)

참고로 해당 화면을 빠져나올때는 :q 키를 누르면 빠져나올수잇다.

 

전체 Database Instance 목록 확인
\list

새로운 db 생성 후 목록 확인 
# 새로운 db 생성
create database 원하는 db명;

# 유저 새로 생성
create user 유저명 with encrypted password '비밀번호';

# db 생성 권한 부여
alter user 유저명 createdb;

# 생성한 유저에게 해당 데이터베이스에 대한 모든 권한 부여
grant all privileges on database db명 to 유저명;

 

\list	#목록 확인

local database 가 생성된 것 확인할 수 있다.

 

# 테이블 리스트 확인
\dt

# 데이터 베이스 연결
\connect db명

# 접속종료 
\q

# -U 명령어와 유저명 입력
psql postgres -U 유저명

postgres-# 에서 postgres=> 으로 변경된걸 확인 할 수 있다. 

 

db 생성후 다시 한번 권한 잘 설정됐는지 확인
\du

생성한 유저이름 까지 추출되는 것을 볼 수 있다.

 

 


dbeaver 데이터 베이스 접속

dbeaver 에서 접속

왼쪽 상단에서 해당 모양의 새 데이터베이스 연결을 선택하면 아래와 같은 페이지가 나온다.

 

 

  • postgreSQL 선택

  • 위에서 자신이 생성한 데이터 베이스에 맞게 database와 Username 란 작성 한 후 완료 선택

  • 나는 local 이란 이름으로 데이터베이스를 생성했기 때문에 local 이란 새로운 데이터 베이스로 접속