본문 바로가기
Programming/PHP

[코드이그나이터] PostgreSQL 사용하기

by Berasix 2023. 2. 9.
반응형

0. PostgreSQL 서버 및 관리툴 설치

오늘 날짜로 최신인 PostgreSQL 15.1을 윈도우 버전으로 설치를 진행했다.

Stack Builder는 일단 패스하고 설치했다.

 

https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

 

Community DL Page

Note: EDB no longer provides Linux installers for PostgreSQL 11 and later versions, and users are encouraged to use the platform-native packages. Version 10.x and below will be supported until their end of life. For more information, please see this blog p

www.enterprisedb.com

 

1. php.ini 파일 설정중 확장기능 확인

PS C:\dev\berasix> php --ini
Configuration File (php.ini) Path: 
Loaded Configuration File:         C:\xampp\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

 

php --ini 명령으로 php.ini 파일을 확인하고, php.ini 파일에서 PostgreSQL 확장 기능 주석을 해제한다.

extension=pgsql

 

2. .env DATABASE 섹션 수정

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = DB이름
database.default.username = postgres   # 혹은 만든 계정
database.default.password = 패스워드
database.default.DBDriver = Postgre
database.default.DBPrefix =
database.default.port = 5432

설정의 포인트는 DBDriver와 port이다.

5432번은 PostgreSQL의 기본 포트 번호이다.

 

3. CodeIgniter Shield 재설치해보기

연결은 되었는데, 테이블을 추가하는 등 마이그레이션을 하려고 해 보니 Mysql과 PostgreSQL의 Data Type 등에 대해서 추가로 공부를 해야 할 것  같아 일단 코드이그나이터 Migration 기능을 사용했다.

 

코드를 수정한 부분이 있기 때문에, 파일들을 Overwrite 하지는 않고, 마이그레이션만 진행했다.

PS C:\dev\berasix> php spark shield:setup

CodeIgniter v4.2.10 Command Line Tool - Server Time: 2023-02-09 12:52:30 UTC+09:00

  File 'APPPATH\Config\Auth.php' already exists in destination. Overwrite? [n, y]: n                                                                                                                                                                
  Skipped APPPATH\Config\Auth.php. If you wish to overwrite, please use the '-f' option or reply 'y' to the prompt.
  File 'APPPATH\Config\AuthGroups.php' already exists in destination. Overwrite? [n, y]: n                                                                                                                                                          
  Skipped APPPATH\Config\AuthGroups.php. If you wish to overwrite, please use the '-f' option or reply 'y' to the prompt.
  Skipped APPPATH\Controllers\BaseController.php. It has already been updated.
  Skipped APPPATH\Config\Routes.php. It has already been updated.
  Security Setup: Everything is fine.
  Run `spark migrate --all` now? [y, n]: y                                                                                                                                                                                                          
Running all new migrations...
        Running: (CodeIgniter\Shield) 2020-12-28-223112_CodeIgniter\Shield\Database\Migrations\CreateAuthTables
        Running: (CodeIgniter\Settings) 2021-07-04-041948_CodeIgniter\Settings\Database\Migrations\CreateSettingsTable
        Running: (CodeIgniter\Settings) 2021-11-14-143905_CodeIgniter\Settings\Database\Migrations\AddContextColumn
Migrations complete.

 

마이그레이션 후 테이블 생성을 확인한다.

 

다른 테이블도 있다면, 데이터베이스 마이그레이션을 사용하는 것이 좋을 것이다.

 

http://ci4doc.cikorea.net/dbmgmt/migration.html?highlight=migration 

 

데이터베이스 마이그레이션 — CodeIgniter 4.2.11 documentation

© Copyright 2019-2022 CodeIgniter Foundation. Last updated on Dec 27, 2022.

ci4doc.cikorea.net

 

4. 데이터 넣어보기

Mysql에서 데이터 덤프로 넣어보는 테스트를 해봤다.

SQLyog를 사용하여 아래처럼 덤프를 이용했다.

insert  into `users`(`id`,`username`,`status`,`status_message`,`active`,`last_active`,`created_at`,`updated_at`,`deleted_at`) values 
(1,'test',NULL,NULL,1,'2023-02-09 11:12:17','2022-11-10 20:05:32','2022-11-10 20:05:32',NULL),
(2,'누구게',NULL,NULL,1,'2022-12-20 00:46:31','2022-11-10 20:17:20','2022-11-10 20:17:20',NULL),
(3,'내가누구게',NULL,NULL,1,'2022-11-10 20:34:13','2022-11-10 20:21:01','2022-11-10 20:21:01',NULL);

이걸 그대로 사용하면 오류가 난다.

"`" 를 제거하고, 아래와 같이 변환해야 제대로 insert 가 된다.

 

insert  into users(id,username,status,status_message,active,last_active,created_at,updated_at,deleted_at) values 
(1,'test',NULL,NULL,1,'2023-02-09 11:12:17','2022-11-10 20:05:32','2022-11-10 20:05:32',NULL),
(2,'누구게',NULL,NULL,1,'2022-12-20 00:46:31','2022-11-10 20:17:20','2022-11-10 20:17:20',NULL),
(3,'내가누구게',NULL,NULL,1,'2022-11-10 20:34:13','2022-11-10 20:21:01','2022-11-10 20:21:01',NULL);

로그인은 최소한 패스워드가 등록되어 있는 auth_identities" 테이블까지 insert 하고 테스트 가능하다. 

 

 

 

이 글은 필자가 분석, 공부하면서 작성한 포스트입니다. 

잘못된 부분에 대해 알려주시면 수정하도록 하겠습니다.

728x90

댓글