반응형

Ubuntu에 PostgreSQL 설치하고 기본명령(Select, Insert Update, Delete)를 살펴봅시다.

먼저 apt-get을 업데이트 해줍니다. 그리고 postgresql을 설치 합니다.

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

postgresql을 설치하면 postgres라는 계정이 생성된다.

postgres 계정으로 변경해 보자. postgres 계정은 postgresql을 관리하는 계정이다.

$ sudo -i -u postgres

postgres 계정으로 변경 후 postgresql로 들어가보자. 명령은 psql이다.

$ psql
psql (9.3.18)
Type "help" for help.

postgres=# 

현재 설치된 버전은 postgresql 9.3.18인걸 알 수 있다.

postgresql에서 나가려면 \q 명령이다.

postgres=# \q

sudo 계정으로 postgresql에 접속하려면 아래처럼 하면 된다.

$ sudo -u postgres psql
psql (9.3.18)
Type "help" for help.

postgres=#

postgres 계정으로 새로운 postgresql 계정을 만들려면 createuser 명령으로 하고 --interactive 플레그를 주면 된다.

명령을 치면 추가할 계정과 superuser인지 묻는다.

$ sudo -i -u postgres
$ createuser --interactive
Enter name of role to add: dejavu
Shall the new role be a superuser? (y/n) y

명령에 대한 설명을 보려면 man 명령으로 확인할 수 있다.

$ man createuser

이제 Database를 생성해 봅시다.

postgres 계정으로 변경하고 createdb 명령으로 만들수 있습니다. dejavu라는 데이터베이스를 만들어 봅시다.

$ sudo -i -u postgres
$ createdb dejavu

postgresql에서는 데이터베이스명과 동일한 linux 유저 계정이 필요합니다.

Ubuntu에서 데이터베이스명과 동일한 계정을 만들어 봅시다.

$ sudo adduser dejavu

postgresql의 데이터베이스명postgres user명Linux 유저 계정이 동일하게 존재해야 한다.

 

이제 dejavu계정으로 dejavu 데이터베이스에 접속하려면 아래처럼 하면 됩니다.

$ sudo -u dejavu psql -d dejavu
psql (9.3.18)
Type "help" for help.

dejavu=# 

접속 후 접속정보를 확인하고 싶다면 \conninfo 명령으로 확인할 수 있다.

$ sudo -u dejavu psql -d dejavu
psql (9.3.18)
Type "help" for help.

dejavu=# \conninfo
You are connected to database "dejavu" as user "dejavu" via socket in "/var/run/postgresql" at port "5432".
dejavu=# 

이제 테이블을 만들어 봅시다.

CREATE TABLE words (
    equipId serial PRIMARY KEY,
    word varchar (50) NOT NULL,
    means varchar (250) NOT NULL,
    example varchar (1000) NULL,
    location varchar (250) check (location in ('north', 'south', 'west', 'east', 'northeast', 'southeast', 'southwest', 'northwest')),
    updateDate date
);

 equipId의 serial 타입은 자동 증가하는 숫자다. location 컬럼의 check 옵션은 해당 옵션값들 중에서만 입력될 수 있다.

데이터베이스의 테이블을 확이하는 명령은 \d이다.

dejavu=# \d
               List of relations
 Schema |       Name        |   Type   | Owner  
--------+-------------------+----------+--------
 public | words             | table    | dejavu
 public | words_equipid_seq | sequence | dejavu
(2 rows)

테이블 리스트에 words 테이블 외에 words_equipid_seq 테이블이 있고 타입으로 sequence로 되어 있다. 해당 테이블은 serial 타입이 있을 경우 자동증가에 대한 데이터를 저장하게 됨다.

타입이 table인 것만 확인하려면 \dt 명령으로 확인할 수 있다.

dejavu=# \dt
        List of relations
 Schema | Name  | Type  | Owner  
--------+-------+-------+--------
 public | words | table | dejavu
(1 row)

생성한 words 테이블에 데이터를 입력하고 출력해 보자.

INSERT INTO words (word, means, example, location, updateDate) VALUES ('simple', '간단한', 'see also simply', 'east', '2017-09-14');
INSERT INTO words (word, means, example, location, updateDate) VALUES ('difficult', '어려운', 'an unreasonable and unhelpful way.', 'west', '2017-09-14');

데이터 확인은 select 문으로 확인한다.

dejavu=# select * from words
dejavu-# ;
 equipid |   word    | means  |              example              | location | updatedate 
---------+-----------+--------+------------------------------------+----------+------------
       1 | simple    | 간단한 | see also simply                    | east     | 2017-09-14
       2 | difficult | 어려운 | an unreasonable and unhelpful way. | west     | 2017-09-14
(2 rows)

where절을 넣어서 검색할 수도 있다.

dejavu=# select * from words where word = 'simple';
 equipid |  word  | means  |    example     | location | updatedate 
---------+--------+--------+-----------------+----------+------------
       1 | simple | 간단한 | see also simply | east     | 2017-09-14
(1 row)

이제 update로 데이터를 변경해 보자.

dejavu=# update words set means = '[^디퍼런스]\n어려운' where word = 'difficult';
UPDATE 1

그리고 다시 select로 확인하면 변경된 것을 확인할 수 있다.

dejavu=# select * from words where word = 'difficult';
 equipid |   word    |        means        |              example              | location | updatedate 
---------+-----------+---------------------+------------------------------------+----------+------------
       2 | difficult | [^디퍼런스]\n어려운 | an unreasonable and unhelpful way. | west     | 2017-09-14
(1 row)

delete문으로 삭제도 해보자.

dejavu=# delete from words where word = 'simple';
DELETE 1

그리고 다시 select로 확인하면 삭제된걸 확인할 수 있다.

dejavu=# select * from words;
 equipid |   word    |        means        |              example              | location | updatedate 
---------+-----------+---------------------+------------------------------------+----------+------------
       2 | difficult | [^디퍼런스]\n어려운 | an unreasonable and unhelpful way. | west     | 2017-09-14
(1 row)

테이블에 컬럼을 추가하거나 삭제하는 것도 확인해 보자.

테이블 변경은 alter 문으로 할 수 있다. lastdate라는 컬럼을 추가하고 select 해보자.

dejavu=# alter table words add lastdate date;
ALTER TABLE
dejavu=# select * from words;
 equipid |   word    |        means        |              example              | location | updatedate | lastdate 
---------+-----------+---------------------+------------------------------------+----------+------------+----------
       2 | difficult | [^디퍼런스]\n어려운 | an unreasonable and unhelpful way. | west     | 2017-09-14 | 
(1 row)

추가할 컬럼을 삭제해 보자.

dejavu=# alter table words drop lastdate;
ALTER TABLE
dejavu=# select * from words;
 equipid |   word    |        means        |              example              | location | updatedate 
---------+-----------+---------------------+------------------------------------+----------+------------
       2 | difficult | [^디퍼런스]\n어려운 | an unreasonable and unhelpful way. | west     | 2017-09-14
(1 row)

이것으로 PostgreSQL 설치와 기본 사용 확인 끝.

반응형

'OS (Operating System) > Linux' 카테고리의 다른 글

Linux 메모리 확인 방법  (0) 2021.02.16
scp, sshpass 사용법, 설치(리눅스 보안)  (0) 2020.12.07
셸 스크립트 문법  (0) 2020.09.25
리눅스 셸(Shell), 환경 변수  (1) 2020.09.25
telnet, ftp 사용법  (0) 2020.09.25
블로그 이미지

Runer

IT / 일상 / 먹방 / 꿀팁 / 유틸

,
반응형

오늘은 Linux에서 postgresql 12 버전에 대한 설치 및 업그레이드 방법에 대해 포스팅 하려고 합니다.

 

먼저 Postgresql이 기존 Linux에서 설치가 되어있다면, 제거하고 재설치 하는 방법으로

진행하겠습니다.

(* 업그레이드가 아닌 설치만 하시는 분들은 5)의 내용부터 진행하시면 됩니다.)

 

1) 기존의 Postgresql 서비스 중지

# systemctl stop postgresql-9.6
# systemctl disable postgresql-9.6 

 

2) 기존의 Postgresql 데이터 삭제

# rm -rf /var/lib/pgsql

 

3) 기존의 Postgresql 계정 제거

# userdel postgres

 

4) 기존의 Postgresql 패키지 제거

# yum remove *postgres*

 

5) yum repository 업데이트

# rpm -Uvh https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

 

6) Postgresql 12버전 설치

# yum -y install postgresql12-server postgresql12-contrib

 

7) Postgresql 기본 데이터베이스 생성

# /usr/pgsql-12/bin/postgresql-12-setup initdb

 

8) Postgresql 12 서비스 등록 및 시작

# systemctl enable postgresql-12
# systemctl start postgresql-12

 

9) Postgresql 계정 생성

# su postgres
# psql postgres
// CREATE USER [계정아이디] PASSWORD '[계정패스워드]' SUPERUSER;
# CREATE USER clovir PASSWORD 'vmware1!' SUPERUSER;

 

10) 외부 접근 허용 설정

- 외부 접근 허용이 될 수 있도록 설정해놔야 다른 IP에서 DB툴 또는 JDBC connection을 하여

Linux 서버에 설치된 Postgresql DB를 제어할 수 있습니다.

// postgresql.conf 파일 수정
 # vi /var/lib/pgsql/12/data/postgresql.conf
 listen_addresses='*'
 으로 수정
 
// pg_hba.conf 파일 수정
 # vi /var/lib/pgsql/12/data/pg_hba.conf
 # TYPE  DATABASE        USER            ADDRESS                 METHOD 이 위치 아래에
 host    all             all             0.0.0.0/0               md5 
 으로 추가

 

11) Postgresql 서비스 재시작

# systemctl restart postgresql-12

 

위 내용이 문제없이 진행이 되었다면, 

# service postgresql-12 status

명령어를 입력 했을 때 아래 이미지와 같이 active 상태로 나타나게 됩니다.

postgresql service 상태 확인

 

또한 아래 이미지와 같이 DB tool을 사용하여 정상적으로 연결 되는것을 확인할 수 있습니다.

 

 

 

지금까지 Linux에서 postgresql 12 버전에 대한 설치 및 업그레이드 방법에 대한

포스팅이였습니다.

반응형
블로그 이미지

Runer

IT / 일상 / 먹방 / 꿀팁 / 유틸

,