LoginSignup
20
15

More than 5 years have passed since last update.

Heroku Postgres を使ってみる

Last updated at Posted at 2016-04-17

手順

1. Heroku のアカウントを用意

2. Heroku PostgresCreate Database をクリックし、表示されたモーダル内の Dev Plan(Free) を選択

HerokuPostgres.png

*※ 10,000 行までは無料で使用することができる *

3. psql で接続

  1. psql -h hostname -U username -d databasename
  2. パスワード入力

4. テーブルを作成し、データを挿入

-- テーブルを作成
CREATE TABLE sample
(
  crttimestamp timestamp with time zone,
  crtuser character varying(20),
  udttimestamp timestamp with time zone,
  udtuser character varying(20),
  id character varying(10) NOT NULL,
  name character varying(100) NOT NULL,
  CONSTRAINT sample_id_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
-- データを挿入
INSERT INTO sample(crttimestamp, crtuser, udttimestamp, udtuser, id, name) VALUES (current_timestamp, 'test', current_timestamp, 'test', '0000000001', 'test001');
INSERT INTO sample(crttimestamp, crtuser, udttimestamp, udtuser, id, name) VALUES (current_timestamp, 'test', current_timestamp, 'test', '0000000002', 'test002');
INSERT INTO sample(crttimestamp, crtuser, udttimestamp, udtuser, id, name) VALUES (current_timestamp, 'test', current_timestamp, 'test', '0000000003', 'test003');
select * from sample;

困ったこと

ssl で接続しないと下記のエラーが発生した。
heroku postgres の pg_hba.conf は編集できないので、SSLモードで接続するようにする。

FATAL: no pg_hba.conf entry for host "[host ip address]", user "[user name]", database "[database name]", SSL off

20
15
1

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
20
15