psql 명령어를 통해서 스크립트를 실행시키는 것은 생각보다 쉽다.
sudo -u postgres psql -a -d [DB이름] -f [스크립트]
-a는 결과 보여주기(echo)이다.
psql 명령어를 통해서 스크립트를 실행시키는 것은 생각보다 쉽다.
sudo -u postgres psql -a -d [DB이름] -f [스크립트]
-a는 결과 보여주기(echo)이다.
특정값이 몇 개 있는지는 count 명령어를 이용하면 되는데 자꾸 숫자만 나오고 어떤게 있는지는 나오지 않았다. 인터넷 서핑을 통해서 찾아본 방법은 다음과 같음. select 뒤에 한 번 써주고, 마지막에 group by 에 또 써주고.
select drug_source_value_edi_code, count(*) from cdm.drug_exposure where drug_source_value_edi_code in (...) GROUP BY drug_source_value_edi_code;
단일 데이터베이스 백업 (스키마 단위 아님)은 pg_dump를 이용하면 간단히 할 수 있다. 다만 나의 경우에는 sudo -u postgres pg_dump 로 실행했는데 이러면 생성된 파일이 권한 문제가 생길 수 있다. 따라서 저장할 폴더 권한을 777로 편하게 미리 바꾸어 둔다.
pg_dump [데이터베이스] > [백업할 파일 이름]
복원은 다음과 같이 할 수 있다. 인터넷 정보에 의하면 이럴 경우 오류가 생기더라도 일단 진행된다고 한다. [데이터베이스]는 미리 생성되어 있어야 한다.
psql [데이터베이스] < [백업한 파일 이름]
vocabulary 테이블을 만들어야해서. 겨우겨우 방법을 찾을 수 있었다. 그런데 알고 봤던이 CDM의 vocabulary 는 이렇게 만드는 것이 아니었다. 내가 필요했던 것은 3가지 였다. 처음은 2개의 테이블에서 공통 key를 이용해서 새로운 형식의 테이블을 만드는 것, 새로운 컬럼을 추가하는 것, 그리고 여러개를 합치는 것이었다.
일단 공통의 컬럼 값으로 구성되어 있으면 union으로 합칠 수 있다.
특정한 값을 가진 새로운 컬럼을 추가하는 것은 있던 컬럼을 select 하면서 ‘DRUG_KOREA_EDI_ATC’ as vocabulary_name 이런 식으로 그냥 값을 입력해 주면 된다.
2개의 테이블의 공통인 정보를 이용하여 합치는 것은 join을 이용하면 된다.
cdm.drug_mapping as a join cdm.drug_atc_mapping_2 as b on a.atc_code = b.concept_code
CREATE TABLE cdm.vocabulary AS
(select edi_code as vocabulary_id, 'DRUG_KOREA_EDI_ATC' as vocabulary_name, 'HIRA_AND_ATHENA' as vocabulary_reference, NULL as vocabulary_version, concept_id as vocabulary_concept_id from cdm.drug_mapping as a join cdm.drug_atc_mapping_2 as b on a.atc_code = b.concept_code)
union
(select concept_code_re as vocabulary_id, 'DISEASE_KCD7_SNOMED' as vocabulary_name, 'ATHENA' as vocabulary_reference, NULL as vocabulary_version, concept_id_2 as vocabulary_concept_id from cdm.condition_mapping as a join cdm.condition_kcd7_snomed_2 as b on a.concept_id = b.concept_id_1)
union
(select distinct measurement_source_value as vocabulary_id, 'HUDH' as vocabulary_name, 'ATHENA' as vocabulary_reference, NULL as vocabulary_version, measurement_concept_id as vocabulary_concept_id from cdm.measurement order by vocabulary_id);