7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

postgreSQLをローカル環境で操作する

Posted at

ローカル環境でpostgreSQLをインストールから操作するまで

久しぶりにpostgreSQLを扱ったら大分忘れていたので、メモがてら残します。

homebrewでpostgreSQLをインストールする。

まず、macのローカル環境にpostgreSQLの環境を構築するためには、homebrewでインストールする。

[homebrewについて詳しくはこちら][link-1]
[link-1]:https://qiita.com/omega999/items/6f65217b81ad3fffe7e6

$ brew install postgresql

ちなみにbrew install以外のやり方も多数あるが、このやり方が一番簡単だと感じました。

逆に、色々ありすぎて最初はどのやり方がベストなのか悩みました。笑

正常にインストールされている場合は、下記コマンドでバージョンが表示される。

$ postgres --version
postgres (PostgreSQL) 9.3.4

サーバーの起動と停止

サーバーの起動と停止についてもやり方がさまざま存在するが、筆者はこのやり方一択です。

postgreSQLサーバーを起動する。

brew services start postgresql

停止する際は下記コマンド

brew services stop postgresql

postgreSQLサーバーの起動状態を確認する。

$ brew services list
Name       Status  User          Plist
mysql      stopped
postgresql started username /Users/username/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

これでインストールとサーバーの起動停止は完了です。

ターミナル上でデータベースを操作する

データベースに入るにはpsqlコマンドを利用する。

$ psql データベース名

データベースの一覧を見たい場合

$ psql -l

 Name    |     Owner     | Encoding | Collate | Ctype |        Access privileges
-----------+---------------+----------+---------+-------+---------------------------------
 test    | tesutdesuyote | UTF8     | C       | C     |
 test    | tesutdesuyote | UTF8     | C       | C     |
 test    | tesutdesuyote | UTF8     | C       | C     |
 test    | tesutdesuyote | UTF8     | C       | C     | =c/tesutdesuyote               +
         |               |          |         |       | tesutdesuyote=CTc/tesutdesuyote
 test    | tesutdesuyote | UTF8     | C       | C     | =c/tesutdesuyote               +
         |               |          |         |       | tesutdesuyote=CTc/tesutdesuyote
(5 rows)

[psqlコマンドについて詳しくはこちら][link2]
[link2]:https://qiita.com/aosho235/items/c657e2fcd15fa0647471

これでローカル環境で一通りpostgreSQLを操作することができるようになる。

ちなみにGUIでデータベースを操作する場合は、posticoやDBeaverといったDBツールがおすすめです。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?