LoginSignup
39
34

More than 5 years have passed since last update.

HerokuのPostgreSQLにログインしてSQLを直接実行する。

Last updated at Posted at 2018-07-12

HerokuでPostgreSQLに接続して直接SQLを実行したい。
heroku pg:psql コマンドを打ってログインできなかったので備忘録として残します。

環境は CentOS7.5です。

$ heroku pg:psql
--> Connecting to postgresql-nipple-114514
 ▸    The local psql command could not be located. For help installing psql, see
 ▸    https://devcenter.heroku.com/articles/heroku-postgresql#local-setup

ローカルの環境に psql コマンドが入っていないといけないみたいです。

PostgreSQLのバージョンを確認する

$ heroku pg:info
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            10.4
Created:               2018-07-12 10:55 UTC
Data Size:             8.2 MB
Tables:                6
Rows:                  5/10000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-nipple-114514

バージョンを揃える必要があるので、herokuに入っているPostgreSQLのバージョンを確認します。10.4が入ってるみたいですね。

PostgreSQL10.4 インストール

https://www.postgresql.jp PostgreSQL公式サイトを参考にインストールする。

$ sudo yum -y install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
$ sudo yum -y install postgresql10
$ psql -V
psql (PostgreSQL) 10.4

PostgreSQL10.4がインストールできました。
psqlコマンドも使えるのでokですね。

$ heroku pg:psql
--> Connecting to postgresql-nipple-114514
psql (10.4)
SSL 接続 (プロトコル: TLSv1.2、暗号化方式: ECDHE-RSA-AES256-GCM-SHA384、ビット長: 256、圧縮: オフ)
"help" でヘルプを表示します。

app-name::DATABASE=>

接続できました!

テーブル一覧

> \d

PostgreSQLって2文字でテーブル一覧表示できるんですね...
すごい...

> SELECT * FROM hogehoge;

SQLも実行できました。

39
34
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
39
34