postgreSQLの実行環境作ったメモ
vagrant と virtual Boxがすでにあったので超簡単
1. 任意のディレクトリを作成(毎回忘れるのでメモーディレクトリは)
2. vagrant init
3. VagrantFileを編集
code:vagrantFile
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provider "virtualbox" do |vb|
vb.memory = "8192"
end
end
initコマンドで雛形が作成されるので、コメントアウトされている箇所を外す&適宜追記するだけ
4. vagrant up
5. vagrant ssh
これでUbuntu環境の準備は完了。そのままpostgreSQLをインストールする。
一応aptを更新してからインストール。バージョン指定しなかったら10.12が入った(2020.5現在は12まででてる)
code:command
$ sudo apt update
$ sudo apt upgrade
$ sudo yum install postgresql-server
$ psql --version
psql (PostgreSQL) 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)
$
postgreSQLの使い方
psql -h ホスト名 -p ポート番号 -U ロール名 -d データベース名
この辺もそうだけど、多分MySQLと大体同じだと思っている。。。
初期状態だと、psql -U postgres でログインできるとのことだったがダメだった
code:login
vagrant@vagrant:~$ su - postgres
## posgre
Password:
vagrant@vagrant:~$ psql -U postgres
psql: FATAL: Peer authentication failed for user "postgres"
vagrant@vagrant:~$ psql -U vagrant
psql: FATAL: role "vagrant" does not exist
vagrant@vagrant:~$
vagrantあるあるというか、実行ユーザーが違うのでpostgresというユーザーでログインできない
なので設定を変更する必要がある
一番目の記事を読んでやろうと思ったけどそれではVMではまだ作業が不足していたので、結局下の記事を読んでやった
やるべきことをまとめると次の通り
postgresユーザーのPWを設定(UNIXとposgere両方)
code:setting.bash
vagrant@vagrant:~$ sudo passwd postgres
Enter new UNIX password:
# section in the documentation for a list of which options are
Retype new UNIX password:
# server for the changes to take effect, run "pg_ctl reload", or execute
passwd: password updated successfully
vagrant@vagrant:~$ su - postgres
Password:
postgres@vagrant:~$ psql
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
# section in the documentation for a list of which options are
Type "help" for help.
postgres=#
postgres=# alter role postgres with password '****';
ALTER ROLE
postgres=# \q
postgresの接続設定を変更する
postgresを再起動 sudo service postgresql restart
設定ファイルはバージョンによって異なるかもしれない。今回は下記の場所を変更した
code:settigfile
vagrant@vagrant:~$ sudo vim /etc/postgresql/10/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all trust
postgreSQLの起動とかその辺
sudo service postgresql start/restart/stop
postgres-# \q posgreからのログアウト
外部ファイルのインポートが必要だったので追加作業
サンプルファイルがあったのでそれをposgreにインポートする必要がある
手順は下記の通り
vagrant->ローカルにssh接続するための設定を~/.ssh/configに追記
localのvagrantのdir $ vagrant ssh-config --host app_server >> ~/.ssh/config
scpでローカルにあるzipファイルをvagrantにおくってunzip(コマンドなかったのでinstall)
$ scp ~/Downloads/stdsql-20150724.zip vagrant@app_server:~/stdsql-20150724.zip
code:unzip.bash
vagrant@vagrant:~$ unzip stdsql-20150724.zip
-bash: unzip: command not found
vagrant@vagrant:~$ sudo apt-get install zip unzip
vagrant@vagrant:~$ unzip stdsql-20150724.zip
bashから勝手にインポートしてくれるらしいファイルを実行する
code:bash
vagrant@vagrant:~$ bash load.command
Password:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
vagrant@vagrant:~$
こけたので設定を見直す。postgresというuserでpsqlが実行できていない様子
posgreのユーザーの設定やり直して成功した!
最初の手順でpsqlの後のユーザー設定までできてなかったので修正して加筆
psql 実行後にalter role postgres with password '****'; したところ
code:posgre
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------------------+-------+----------
public | access_log | table | postgres
public | access_log_dyn | table | postgres
public | access_log_wide | table | postgres
public | customer_category_mapping | table | postgres
public | customer_locations | table | postgres
public | customers | table | postgres
public | items | table | postgres
public | monthly_sales | table | postgres
public | order_details | table | postgres
public | order_details_h | table | postgres
public | order_details_v | table | postgres
public | orders | table | postgres
public | pivot_table | table | postgres
public | search_log_dyn | table | postgres
public | shops | table | postgres
public | web_pages | table | postgres
public | yearly_orders | table | postgres
public | yearly_sales | table | postgres
(18 rows)
postgres=#