35
28

More than 5 years have passed since last update.

はじめに

とりあえずブラストワンピース有馬記念勝利おめでとう:sunny:

それで今回はpostgresqlの最初の設定について忘備録として書いていく。

osはubuntu。

postgresqlインストール

postgresqlをインストールするには

sudo apt-get install postgresql

でOK

そのあと

sudo passwd postgres

でパスワードを設定する。

sqlサーバーに接続

sqlサーバーに接続する

su postgres

のあとパスワードが求められるから入力、そのあと

psql

と入力すれば

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"?

こんなエラーがでた。

そもそもs.PGSQL.5432なんてファイルないしどうしよう…と思って調べていたが動やらこのエラーはsqlのサーバーを起動していないために出るらしい。

なのでこのエラーがでたら

sudo /etc/init.d/postgresql start

と打てばOK

無事sqlが起動されエラーが出なくなるはず。

createuserができない

とりあえず設定は終わったと思ってcreateuser -d -P nameとしてrailsのアプリで使うユーザを作成しようと思ったらこんなエラーがでた

createuser: could not connect to database postgres: FATAL:  role "root" does not exist

どうやらrootというroleがないことが問題らしい

ロールの作成の仕方は

su postgres
psql

と打って表示がpostgres=#となったら

CREATE ROLE rolename LOGIN CREATEDB CREATEROLE PASSWORD 'password';

とすればOK

CREATEROLEを書かないと

createuser: creation of new role failed: ERROR:  permission denied to create role

このように怒られる。

ちなみにロールの情報を変更したい場合は

ALTER ROLE name WITH CREATEROLE 

のように変更できる。

railsでpostgresqlをつかう

railsでpostgresqlを使う場合

まず

createuser usernmae
createdb dbname

としてuserとdbを作成する

rails newするときに

rails new name -TB --database=postgresql

とすることでpostgresqlをつかうrailsアプリを作れる。

config/database.ymlのdefault: のところに

username: username
password: pass
host: localhost

を追加で書き込む

developmentのところの

database: dbname

も書き換える。

これでrailsでpostgresqlを使うことができるようになった:sunny:

参考文献

インストールからRails-PostgreSQL環境を整える
https://qiita.com/torini/items/9952d91c4a7087b23481

Rails で postgresql を使う(インストールからマイグレーションまで)
https://qiita.com/longtime1116/items/9045717ff8607bed07fe

35
28
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
35
28