ITの隊長のブログ

ITの隊長のブログです。Rubyを使って仕事しています。最近も色々やっているお(^ω^ = ^ω^)

dockerでpostgresqlを起動して遊ぶ

まだ遊べていないのでログ残し

qiita.com

自分はpullで対応

$ docker pull postgresql:10.5

で、runしようとすると

$ docker run --rm --name=postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -it postgres psql -U postgres
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

なんやねんこれ?

/bin/bashしてみて、それで起動してみる.

$ docker run --rm --name=postgres -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -it postgres /bin/bash
root@badb9c06d26c:/#

root@badb9c06d26c:/# ps aux | grep postgres

どうやら起動してない模様

root@badb9c06d26c:/# service postgresql start
[warn] No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning).

なるほど? ぐぐる

github.com

Ran following commands, and after that Postgres was running.

root@ee38e70bca23:/# dpkg-reconfigure locales root@ee38e70bca23:/# pg_createcluster 9.5 main --start root@ee38e70bca23:/# /etc/init.d/postgresql start

なるほど。手順通りたたく(バージョンは調整した

すると起動した!が、しかし、ログインに失敗する。

眠たいので続きは明日.

続き

上の原因はrootログインで失敗していたので

  1. su - postgresでpostgresユーザーに変更
  2. psql -U postgresでログイン、パスワード変更
  3. rootに戻り、postgres_hba.confmd5, trustに変更する。本番ではtrustは良くない
  4. postgres再起動する

メンドクサ!!!!ってなったのでimages削除して下記を対応したらできた。

qiita.com

つらい

volume作らないとコンテナ削除したとき一緒にデータが消えてしまうので、volumeを作る

qiita.com

DB作成とかテーブル作成はguiのclientアプリを使ったら簡単

qiita.com

以上、これでできた

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

X_reduced = TSNE(n_components=2, random_state=0).fit_transform(X)
_, ax = plt.subplots(figsize=(12, 9))
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=data['label'])
plt.colorbar()

t-sneで次元削減したデータをscatterでplotしようとしたらエラーがでた。

ぐぐると、下記で解決したがいまいちよくわかっていない

stackoverflow.com

X_reduced = TSNE(n_components=2, random_state=0).fit_transform(X)
_, ax = plt.subplots(figsize=(12, 9))
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], c=data['label'])
pcm = ax.get_children()[0]
plt.colorbar(pcm, ax=ax)
ax.get_children()
[<matplotlib.collections.PathCollection at 0x7f41e2b26fd0>,
 <matplotlib.spines.Spine at 0x7f41e25c4cf8>,
 <matplotlib.spines.Spine at 0x7f41e25c4ba8>,
 <matplotlib.spines.Spine at 0x7f41e25c49b0>,
 <matplotlib.spines.Spine at 0x7f41e25c4940>,
 <matplotlib.axis.XAxis at 0x7f41e25c44a8>,
 <matplotlib.axis.YAxis at 0x7f41e261f160>,
 Text(0.5,1,''),
 Text(0,1,''),
 Text(1,1,''),
 <matplotlib.patches.Rectangle at 0x7f41e28d3470>]

ax.get_children()をみてみると、こんな感じ↑でオブジェクトがあるので、0番目のオブジェクトを渡すとうまくいった。ということを覚えておく(理解はしていない