don't stop believing

Ubuntu에 PostgreSQL 설치하고 기본 명령 살펴보기 본문

Database/PostgreSQL

Ubuntu에 PostgreSQL 설치하고 기본 명령 살펴보기

Tongchun 2017. 9. 14. 13:54

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 설치와 기본 사용 확인 끝.


'Database > PostgreSQL' 카테고리의 다른 글

외부 서버에서 postgresql 접속하기  (1) 2017.09.27
sql 파일 실행시키기  (0) 2017.09.27
alter table 사용하기  (0) 2017.09.26
Table 관련 기본 다루기  (0) 2017.09.26
postgresql 데이터 타입과 옵션  (0) 2017.09.14
Comments