LoginSignup
1

More than 5 years have passed since last update.

posted at

updated at

PostgreSQLテンプレートのUTF8化をスクリプトで行う

概要

Cloud9でPostgreSQLを利用する場合毎回templateの更新が必要なのが面倒になってきたのでスクリプト化します。

準備

Cloud9にてRuby on Railsのプロジェクトテンプレートを選択し新しいワークスペースを作成します。

$ sudo service postgresql start
# python2のpsycopg2はすんなり入らなかったのでpython3を利用します
$ sudo pip3 install psycopg2

スクリプト

change_template.py
import psycopg2
import psycopg2.extras

connect = psycopg2.connect(dbname='postgres')
cursor = connect.cursor(cursor_factory=psycopg2.extras.DictCursor)
old_isolation_level = connect.isolation_level
connect.set_isolation_level(0)

cursor.execute("UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1'")
cursor.execute("DROP DATABASE template1")
cursor.execute("CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE'")
cursor.execute("UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1'")
cursor.execute("VACUUM FREEZE")

app_name = "my_app_name"
password = "pass"

cursor.execute("create role " + app_name + " with createdb login password '" + password + "'")

connect.commit()
connect.set_isolation_level(old_isolation_level)

postgrsユーザでこのスクリプトを実行します。

$ sudo sudo -u postgres python3 change_template.py

動作確認

psqlを起動してtemplate1のEncodingがUTF8になっていることを確認します。

$ sudo sudo -u postgres psql
ユーザ情報を確認
\du
  Role name  |                   Attributes                   | Member of 
-------------+------------------------------------------------+-----------
 my_app_name | Create DB                                      | {}
 postgres    | Superuser, Create role, Create DB, Replication | {}
 ubuntu      | Superuser, Create role, Create DB              | {}
データベースを確認
\l
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8      | C       | C     | 
 ubuntu    | ubuntu   | SQL_ASCII | C       | C     | 

Rails

ここからはデフォルトのsqlite3をpgに置き換えて動作確認を行います。

Gemfile
gem 'pg'
$ bundle install
config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: my_app_name
  password: pass
  host: localhost

development:
  <<: *default
  database: my_app_name_development

test:
  <<: *default
  database: my_app_name_test

production:
  <<: *default
  database: my_app_name_production
$ rails g scaffold Post title:string body:text
$ rake db:migrate
$ rails s -b $IP -p $PORT

cloud9のポップするURLをクリックするか、上のメニューからPreview -> Preview Running Applicationを選択しrailsを表示させます。
または直接
https://workspaceName-UserName.c9users.io/posts
で確認できます。

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
What you can do with signing up
1