PostgreSQL은 오픈소스 관계형 데이터베이스(RDBMS)로, 강력한 기능과 안정성을 바탕으로 많은 서비스에서 사용되고 있습니다.
이 글에서는 PostgreSQL을 처음 접하는 분들을 위해 기본 명령어들을 정리해보았습니다.
🔍 1. PostgreSQL 접속 명령어
psql -U [사용자명] -d [데이터베이스명] -h [호스트] -p [포트]
또는
$PG_HOME/bin/pg_ctl -D $PGDATA start|stop|restart|status
🔍 2. psql 메타 명령어(내부 명령어)
postgres=# \c {DB_NAME} # 다른 DB 접속
postgres=# \l # 데이터베이스 조회
postgres=# \du # User 권한, 로그인 여부 등 조회
postgres=# \dn # Schema 조회
postgres=# \db # Tablespace 조회
postgres=# \dt # 접속한 DB 테이블 조회
postgres=# \dv # 접속한 DB의 VIEW 조회
postgres=# \d # 접속한 DB의 테이블 상세정보
postgres=# \g # 이전 명령어 실행
postgres=# \s # history 조회
postgres=# \x # 결과출력 컬럼 배치 변경
postgres=# \timing # 쿼리 실행시간 표시
예시)
postgres=# \c {DB_NAME} # 접속할 DB 변경
postgres=# \c safer
You are now connected to database "safer" as user "postgres".
postgres=# \l # 데이터베이스 조회
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
safer | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
postgres=# \dt # 접속한 DB 테이블 조회
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | fcmtoken | table | postgres
public | safer_log | table | postgres
(2 rows)
postgres=# \du # 접속한 DB의 user 권한, 로그인 여부 등 조회
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
postgres=# \d # 접속한 DB의 테이블 상세정보
List of relations
Schema | Name | Type | Owner
--------+-----------------+----------+----------
public | fcmtoken | table | postgres
public | fcmtoken_no_seq | sequence | postgres
public | safer_log | table | postgres
(3 rows)
postgres=# \x # 결과출력 컬럼 배치 변경
safer=# select * from {TABLE};
-[ RECORD 1 ]-+----------------------------------------------------------------------------------------------------
--------------------------------------------------
message | help
phone_number | 1066146602
latitude | 37.4219983
longitude | -122.084
include_gps | t
include_title | t
send_time | 2023-08-11 06:58:58.776194
msg_state | SmsMessageState.Sending
-[ RECORD 2 ]-+----------------------------------------------------------------------------------------------------
--------------------------------------------------
message | help
phone_number | 1066146602
latitude | 37.4219983
longitude | -122.084
include_gps | t
include_title | t
send_time | 2023-08-11 07:01:32.071397
msg_state | SmsMessageState.Sending
-[ RECORD 3 ]-+----------------------------------------------------------------------------------------------------
--------------------------------------------------
message | sjsjsjsjsjsjsjsjs
phone_number | 1066146602
latitude | 37.4219983
longitude | -122.084
include_gps | t
include_title | t'🗄️ DB_이야기 > # 🐘 PostgreSQL' 카테고리의 다른 글
| 🐘[PostgreSQL] DB Link(DB접근) (1) | 2025.08.01 |
|---|---|
| 🐘[PostgreSQL] 유저(User) 생성, 변경, 삭제, 조회 (1) | 2025.08.01 |
| 🐘[PostgreSQL] 외부 접속 허용 방법 (3) | 2025.07.30 |
| 🐘[PostgreSQL] 환경변수 및 DB 기동/정지 정리 (2) | 2025.07.28 |
| 🐘 [PostgreSQL] 기동 실패 에러: "could not bind IPv4 address" (3) | 2025.07.24 |