데이터베이스

Postgresql / pqAdmin4 시작하기(사용법)

meno1011 2022. 3. 19. 01:32
728x90

버전: "PostgreSQL 14.2, compiled by Visual C++ build 1914, 64-bit"

select version(); -- 버전 확인 쿼리문

상단 탭

Object > Create > Server... 을 들어가면 Name은 비어있는 상태로 나타난다.

로컬에서 사용할거라 Local로 정했다.

처음 서버 생성시 General

그 다음 Connection에서 Host name/address에는 localhost를 입력,

Password에는 postgresql 다운받을때 설정한 비밀번호 설정 후

Save password 토글 버튼을 활성화 해준다.

처음 서버 생성시 Connection

Save를 눌러서 서버를 생성하면 아래와 같이 서버가 생성된 것을 확인할 수 있다.

서버 생성

이제 쿼리문을 입력해서 데이터베이스를 조회해보자.

상단 탭에서 Tools > Query Tool를 클릭하면 아래와 같은 화면이 나올거다.

쿼리 툴(Query Editor)

전체 데이터베이스 조회

SELECT datname FROM pg_database; -- 전체 데이터베이스 조회

전체 데이터베이스 조회

유저가 생성한 데이터베이스만 조회

select datname from pg_database where datistemplate = false; --유저가 생성한 데이터베이스만 조회

생성된 데이터베이스 확인

왼쪽 탭에 Databases>postgres에서 볼 수 있듯이

쿼리로 조회한 결과로도 database명이 postgres라는것을 확인 할 수 있다.

그밖에 스키마, 테이블을 조회할 수 있다.

select nspname from pg_catalog.pg_namespace; -- 현재 db의 전체 스키마 조회
select tablename from pg_tables; -- 전체 테이블 조회

・테이블 생성

create table users(
	_id SERIAL primary key,
	name varchar,
	age int
);

postgresql에서 SERIAL은 mysql에서 auto_increment와 같이 자동증가한다

형식은 아래 3종류가 있다.

smallserial 2 바이트 1~32767 serial2
serial 4 바이트 1~2147483647 serial4
bigserial 8 바이트 1~9223372036854775807 serial8

 

・데이터 삽입

_id값은 자동 증가됨으로 name과 age값만 넣어주면 자동으로 _id값은 1부터 삽입되게 된다.

insert into users(name, age) values('meno', 25);
insert into users(name, age) values('tono', 30);

・데이터 조회

select * from users;

데이터 조회

・데이터 업데이트

update users set age=27 where name='meno';
select * from users order by _id asc;

데이터 업데이트 후 조회

age 가 25에서 27로 변경된 값을 확인 할 수 있다.

・데이터 삭제

delete from users where name='tono';
select * from users;

데이터 삭제

데이터 name='tono'가 삭제된 것을 확인 할 수 있다.