본문 바로가기
Database/PGSQL

pssql 시작하기

by 반화넬 2007. 6. 4.
반응형

PostgreSQL을 시작하면서...

2004년 10월 초.. PostgreSQL책을 별 기대감 없이 구입했다. 책의 목차를 둘러보고는 기쁨을 감출 수가 없었다. 열심히 판매되고 있는 상용DBMS와 견주어 공짜 DBMS라는 오명(?)은 나에게서 사라지고 말았다. 상용 DBMS에서 지원하고 있는 기능을 대부분 지원하고 있으며, 특정 DBMS는 없는 기능을 제공 하고 있었다.

이 홈페이지에 있는 문서들은 기본은 제외할 것이다. 기본기능에 대한 소개로 우리가 익히 알고 있는 SELECt, UPDATE, DELETE, INSERT, GROUP BY, HAVING등과 같은 기본키워드는 이러한 기능이 제공되고 있다는 것만을 보여줄 것이다.

많은 부분을 프로시저와 함수, 그리고 트리거에 할당할 것이다. 또한 내장SQL에 대해서도 많은 양을 할당할 것이다.

실제로 야시는 PostgreSQL을 처음 설치해 보았다. 그리고 처음 접하는 것이다. 알고 있는 것이라고는 기존에 다른 DBMS를 다뤘던 것과 데이터베이스의 기초이론 뿐이다. 역시 백문이불여일타!! 한번 해보는 것이 중요하다.

마우스로 뚝딱거려봤더니 뭔가 석연치 않은 것이 역시 윈도우 환경에서는 쬐끔 힘들구나라는 생각을 해보았다. 그러나 세계의 PostgreSQL 개발자들이 논것은 아니다. 다 길이 있다.

사용자 생성

문법은 다음과 같다.

CREATE USER name [ [ WITH ] option [ ... ] ]

where option can be:

SYSID uid
| [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'
| CREATEDB | NOCREATEDB
| CREATEUSER | NOCREATEUSER
| IN GROUP groupname [, ...]
| VALID UNTIL 'abstime'


c:\>psql template1 postgres
Password:
Welcome to psql 8.0.0beta1, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

template1=# create user yasi
template1-# with sysid 5001 password '1234'
template1-# valid until 'infinity';
CREATE USER
template1=# create user sibal
template1-# with sysid 5002
template1-# valid until 'infinity';
CREATE USER

이제 생성한 사용자에 대한 그룹을 생성하도록 하자.

template1=# create group "user"
template1-# with sysid 5000
template1-# user yasi;
CREATE GROUP

sibal 사용자도 user 그룹에 속하도록 해보자. 그리고 sibal사용자에 대한 패스워드도 설정하자.

template1=# alter group "user" add user sibal;
ALTER GROUP
template1=# alter user sibal with password '1234';
ALTER USER

데이터베이스 만들기

문법은 다음과 같다.

CREATE DATABASE name
[ [ WITH ] [ OWNER [=] dbowner ]
[ LOCATION [=] 'dbpath' ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ] ]

다음과 같이 psql을 이용하여 template1 데이터베이스로 접근한다.

c:\>psql template1 postgres
Password:
Welcome to psql 8.0.0beta1, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

template1=#

데이터베이스를 만들기 전에 먼저 테이블스페이스를 만든다. 테이블스페이스는 논리적인 데이터베이스 영역이며, 여러개의 파일을 포함한다. 논리적으로 볼 때 사용자는 하나의 테이블스페이스에 접근하는 것처럼 보이지만 실제로는 여러개의 파일에 접근하는 것이다. 짜증나게도 도움말에 테이블스페이스의 문법이 없다.


테이블스페이스를 만드려면 먼저 경로에 폴더를 하나 만다.

template1=# CREATE TABLESPACE UserTBS
template1-# OWNER postgres
template1-# LOCATION 'C:\\PostgresData';
CREATE TABLESPACE

이제 데이터베이스를 만들어 보도록 하자.
명령프롬프트에서 createdb 명령어를 실행한다. 주의할 것은 template1에 사용자가 없어야 한다는 것이다. 만약 다른 사용자가 접근중이라면 다음과 같은 메세지를 뿌린다.

c:\>createdb -DUserTBS -EEUC_KR -Oyasi -Uyasi UserDB
Password:
createdb: database creation failed: ERROR: source database "template1" is being accessed by other users
쩝...어쨌든 설치하자마자 다른 데이터베이스를 생성해 주어야 한다.


c:\>createdb --help
createdb creates a PostgreSQL database.

Usage:
createdb [OPTION]... [DBNAME] [DESCRIPTION]

Options:
-D, --tablespace=TABLESPACE default tablespace for the database
-E, --encoding=ENCODING encoding for the database
-O, --owner=OWNER database user to own the new database
-T, --template=TEMPLATE template database to copy
-e, --echo show the commands being sent to the server
-q, --quiet don't write any messages
--help show this help, then exit
--version output version information, then exit

Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-W, --password prompt for password

By default, a database with the same name as the current user is created.

Report bugs to .

c:\>createdb -DUserTBS -EEUC_KR -Oyasi -Uyasi UserDB
Password:
CREATE DATABASE

c:\>

생성한 DB에 접근해 보도록 하자.

c:\>psql UserDB yasi
Password:
Welcome to psql 8.0.0beta1, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

UserDB=>

잘된다..우헐헐~

테스트를 위한 데이터넣기


반응형