서버를 만들었으니 이제 디비를 연결해 보자
데이터 베이스는 PostgreSQL 을 쓰겟다. (Fluent 가 지원해서)
목표
- VM에 PostgreSQL 디비를 설치
- 내 맥에 PostgreSQL 클라이언트 앱에서 디비 연결
- 예제 디비 만들어서 서버 반영
- 서버에서 디비값 쿼리해서 HelloWorld 말고 디비값 출력
- 디비 연결 완성
저번에 올려놨던 Vapor 프로젝트를 깃헙에서 내 맥북으로 클론한다.
그리고 터미널로 해당 디렉토리에 들어가
open Package.swift
하면 프로젝트를 Xcode에서 열 수 있다.
패키지 디펜던시를 fetching 하는데 시간이 좀 걸리긴 한다.
다 진행되면 화살표 모양이 뜬다.
디비 세팅하기전에 먼저 코드를 보자면
routes.swift
import Fluent
import Vapor
func routes(_ app: Application) throws {
app.get { req in
return "It works!"
}
app.get("hello") { req -> String in
return "Hello, world!"
}
try app.register(collection: TodoController())
}
웹에서 띄우던 Hello, World 코드를 확인할 수 있다.
Configure.swift 에서 먼저 디비 세팅부터 한다.
기본코드
import Fluent
import FluentPostgresDriver
import Vapor
// configures your application
public func configure(_ app: Application) throws {
// uncomment to serve files from /Public folder
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
app.databases.use(.postgres(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
database: Environment.get("DATABASE_NAME") ?? "vapor_database"
), as: .psql)
app.migrations.add(CreateTodo())
// register routes
try routes(app)
}
보니까 Postgres 디비를 일단 서버에 설치해야될거같다.
서버 힘들어 하니 Nohup 으로 일단 vapor 종료
ps -ef
로 실행중인 프로세스 중 vapor 프로세스 pid 찾아서
kill 13504
서버에 PostgreSQL 설치하기
일단 작업하던 VM 인스턴스에 SSH 로 접속한다.
Ubuntu18 버전에 PostgreSQL 을 설치해야 한다.
ubuntu18 postgresql - Google 검색
2018. 5. 6. · PostgreSQL is the most advanced open source relational database, MySQL did not have triggers, PostgreSQL did. In this tutorial we will see How ...
www.google.com
sudo apt update
sudo apt install postgresql postgresql-contrib
비밀번호 설정
데이터 베이스를 만들자
sudo -u postgres psql postgres
createdb testdb
\list
Postgre 디비에 원격 접속 허용해주자
/etc/postgresql/10/main
이 디렉토리로 이동한다음
sudo vi postgresql.conf
conf 파일을 수정해주자 (LocalHost -> * )
(앞에 # 주석 지워야함 안지워서 net-stat으로 포트 확인하기까지 30분걸림)
그리고
/etc/postgresql/10/main/pg_hba.conf
이 파일도 수정
그리고 포트 열어주고 디비를 껏다키자
sudo ufw allow 5432/tcp
sudo systemctl restart postgresql
Dbeaver 에서 원격 디비에 접속해보자
[여기서 비버 다운로드]
Download | DBeaver Community
Download Tested and verified for MS Windows 7/8/10, Linux and Mac OS X. DBeaver requires Java 1.8 or higher. Windows and MacOS installers include OpenJDK 11 bundle. If you are on Mac OS X and you do NOT use PKG installer then you will need to setup Java. N
dbeaver.io
5432 포트가 0.0.0.0 으로 열려있는지 확인해보고 아니면 conf 파일 다시 수정해야한다.
계속 Connection Timeout 이 뜬다.
디비는 실행 잘 되고 있는데
한번에 되는게 없네
https://dejavuqa.tistory.com/32
외부 서버에서 postgresql 접속하기
PostgreSQL을 기본 설치하면 외부에서는 접속할 수가 없다. config를 수정해 줘야 한다. 우선 Ubuntu에서 열려있는 포트를 확인해 보자. 포트 확인은 netstat -ntlp로 확인하자 $ netstat -ntlp (Not all processe..
dejavuqa.tistory.com
Setting up a remote Postgres database server on Ubuntu 18.04 - LogRocket Blog
We'll look at how to configure a Postgres database server for remote access. With this knowledge, you can set up a database server for your next project
blog.logrocket.com
https://stackoverflow.com/questions/47794979/connecting-to-postgres-server-on-google-compute-engine
connecting to postgres server on google compute engine
I have installed a PostgreSQL on google cloud and I want to be able to access it remotely from my PC using preferably pg-admin. I do get the following error when I try to connect to my instance:
stackoverflow.com
https://stackoverflow.com/questions/42403071/connecting-dbeaver-to-postgres-hosted-on-remote-server
Connecting dbeaver to postgres hosted on remote server
We are using postgres db hosted two servers away, (pardon my jargon) we usually connect through terminal like: local =>(using google auth) ssh server 1 =>(using google auth) ssh server 2 => psql -h
stackoverflow.com
https://browndwarf.tistory.com/51
알아두면 유용한 psql 명령어 정리
PSQL 보통 PostgreSQL을 설치할 때 Client Tool인 pgAdmin이 같이 설치되고, 대부분 GUI 환경에서 pgAdmin을 사용하기 때문에 PSQL의 존재조차 모를 때가 있다. (필자는 PostgreSQL 처음 사용했을 때 psql의 존재..
browndwarf.tistory.com
https://dejavuqa.tistory.com/16
Ubuntu에 PostgreSQL 설치하고 기본 명령 살펴보기
Ubuntu에 PostgreSQL 설치하고 기본명령(Select, Insert Update, Delete)를 살펴봅시다. 먼저 apt-get을 업데이트 해줍니다. 그리고 postgresql을 설치 합니다. $ sudo apt-get update $ sudo apt-get install pos..
dejavuqa.tistory.com
'Server > Vapor - Server_Side_Swift' 카테고리의 다른 글
Vapor 서버 만들기 - Ubuntu+Swift+Vapor+Github+Supervisor(5) (0) | 2020.08.16 |
---|---|
Vapor 서버 만들기 - Ubuntu+Swift+Vapor+Github(4) (0) | 2020.08.16 |
GCP - 피땀흘린 서버 인스턴스 이미지로 백업하기 (0) | 2020.08.16 |
GCP - 무료 Vapor 서버 만들기 - Ubuntu+Swift+Vapor(3) (0) | 2020.08.15 |
GCP - 무료 Vapor 서버 만들기 - Ubuntu+Swift(2) (0) | 2020.08.15 |
댓글