Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PostgreSQL] uuid 생성 #77

Closed
yhshin0 opened this issue Jul 2, 2021 · 0 comments
Closed

[PostgreSQL] uuid 생성 #77

yhshin0 opened this issue Jul 2, 2021 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@yhshin0
Copy link
Contributor

yhshin0 commented Jul 2, 2021

문제 사항

현재 프로젝트에서 생성되는 데이터베이스에 대해 uuid 관련 extension을 설치할 수 있도록 해야 합니다.

관련 정보

PostgreSQL에서의 uuid

PostgreSQL은 uuid를 자동으로 생성하는 기능이 있습니다.

그리고 TypeORM에서 @PrimaryGeneratedColumn("uuid") 데코레이터를 사용하여 uuid를 primary key로 생성할 수 있습니다.

또한, @PrimaryGeneratedColumn("uuid")은 자동으로 생성하고 저장해줍니다.

단, 해당 데이터베이스에서 uuid 생성 함수를 호출하기 위해 관련 extension을 설치해야 합니다.

설치되어 있지 않다면 typeORM 실행시 다음과 같은 에러가 발생합니다.

[Nest] 41362   - 07/02/2021, 9:15:39 PM   [ExceptionHandler] function uuid_generate_v4() does not exist +0ms
QueryFailedError: function uuid_generate_v4() does not exist
    at new QueryFailedError (/goinfre/yshin/ft_transcendence/back/node_modules/typeorm/error/QueryFailedError.js:12:28)
    at PostgresQueryRunner.<anonymous> (/goinfre/yshin/ft_transcendence/back/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:248:31)
    at step (/goinfre/yshin/ft_transcendence/back/node_modules/typeorm/node_modules/tslib/tslib.js:143:27)
    at Object.throw (/goinfre/yshin/ft_transcendence/back/node_modules/typeorm/node_modules/tslib/tslib.js:124:57)
    at rejected (/goinfre/yshin/ft_transcendence/back/node_modules/typeorm/node_modules/tslib/tslib.js:115:69)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

extension 설치

extension 설치는 다음의 명령어로 psql 콘솔에서 수행합니다.

이때, 명령어를 실행하는 유저는 해당 데이터베이스에 대한 소유권을 가진 owner여야 합니다.

database=> CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION
database=> \df
                                 List of functions
 Schema |        Name        | Result data type |    Argument data types    | Type
--------+--------------------+------------------+---------------------------+------
 public | uuid_generate_v1   | uuid             |                           | func
 public | uuid_generate_v1mc | uuid             |                           | func
 public | uuid_generate_v3   | uuid             | namespace uuid, name text | func
 public | uuid_generate_v4   | uuid             |                           | func
 public | uuid_generate_v5   | uuid             | namespace uuid, name text | func
 public | uuid_nil           | uuid             |                           | func
 public | uuid_ns_dns        | uuid             |                           | func
 public | uuid_ns_oid        | uuid             |                           | func
 public | uuid_ns_url        | uuid             |                           | func
 public | uuid_ns_x500       | uuid             |                           | func
(10 rows)

https://stackoverflow.com/questions/22446478/extension-exists-but-uuid-generate-v4-fails

데이터베이스 소유자 변경

데이터베이스에 대한 소유자를 변경하는 명령어는 다음과 같습니다.

ALTER DATABASE name OWNER TO new_owner;

postgres=# alter database "database" owner to "user";
ALTER DATABASE
postgres=# \l
                           List of databases
   Name    |  Owner   | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-------------------
 database  | user     | UTF8     | C       | C     |
 postgres  | yshin    | UTF8     | C       | C     |
 template0 | yshin    | UTF8     | C       | C     | =c/yshin         +
           |          |          |         |       | yshin=CTc/yshin
 template1 | yshin    | UTF8     | C       | C     | =c/yshin         +
           |          |          |         |       | yshin=CTc/yshin
 testdb    | testuser | UTF8     | C       | C     |
(5 rows)

https://stackoverflow.com/questions/4313323/how-to-change-owner-of-postgresql-database/4313625

@yhshin0 yhshin0 added the documentation Improvements or additions to documentation label Jul 2, 2021
@yhshin0 yhshin0 closed this as completed Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant