「Windows10」コマンドプロンプトから PostgreSQL 13へ接続する方法

環境
PostgreSQL 13.2
Windows 10 Home 64bit

操作方法
1.コマンドプロンプトを起動します。

2.「psql -U postgres」と入力してEnterキーを押下します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Program Files\PostgreSQL\13\bin>psql -U postgres
ユーザ postgres のパスワード:
C:\Program Files\PostgreSQL\13\bin>psql -U postgres ユーザ postgres のパスワード:
C:\Program Files\PostgreSQL\13\bin>psql -U postgres
ユーザ postgres のパスワード:

3.パスワードを入力してEnter を押下します。

4.ログインに成功するとプロンプトが「postgres=#」になります。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Program Files\PostgreSQL\13\bin>psql -U postgres
ユーザ postgres のパスワード:
psql (13.2)
"help"でヘルプを表示します。
postgres=#
C:\Program Files\PostgreSQL\13\bin>psql -U postgres ユーザ postgres のパスワード: psql (13.2) "help"でヘルプを表示します。 postgres=#
C:\Program Files\PostgreSQL\13\bin>psql -U postgres
ユーザ postgres のパスワード:
psql (13.2)
"help"でヘルプを表示します。

postgres=#

5.データベースを指定してログインする場合は「-d」オプションを付加して接続します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\Program Files\PostgreSQL\13\bin>psql -U postgres -d testdb
ユーザ postgres のパスワード: #パスワード入力
psql (13.2)
"help"でヘルプを表示します。
testdb=#
C:\Program Files\PostgreSQL\13\bin>psql -U postgres -d testdb ユーザ postgres のパスワード: #パスワード入力 psql (13.2) "help"でヘルプを表示します。 testdb=#
C:\Program Files\PostgreSQL\13\bin>psql -U postgres -d testdb
ユーザ postgres のパスワード: #パスワード入力
psql (13.2)
"help"でヘルプを表示します。

testdb=#

6.日付、時刻を取得します。
SQL構文
select current_date , current_time ;
実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# select current_date , current_time ;
current_date | current_time
--------------+--------------------
2022-09-16 | 06:40:13.709114+09
(1)
testdb=# select current_date , current_time ; current_date | current_time --------------+-------------------- 2022-09-16 | 06:40:13.709114+09 (1 行)
testdb=# select current_date , current_time ;
 current_date |    current_time
--------------+--------------------
 2022-09-16   | 06:40:13.709114+09
(1 行)

7.psql プロンプトを終了する際は「¥q」で閉じます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# \q
C:\Program Files\PostgreSQL\13\bin>
testdb=# \q C:\Program Files\PostgreSQL\13\bin>
testdb=# \q

C:\Program Files\PostgreSQL\13\bin>

 

PostgreSQL

Posted by arkgame