RailsでDB接続する
config/database.ymlに設定した接続情報を元に接続
$ rails dbconsole
db_name=>
postgresのコマンド
データベース一覧
db_name=> \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -------------+----------+----------+-------------+-------------+------------------- db_name | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | user_name | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/user_name + | | | | | user_name=CTc/user_name template1 | user_name | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/user_name + | | | | | user_name=CTc/user_name
ユーザー一覧表示
db_name=> \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- postgres | Create DB | {} user_name | Superuser, Create role, Create DB, Replication | {}
テーブル一覧
db_name=> \d List of relations Schema | Name | Type | Owner --------+------------------------------+----------+---------- public | groups | table | postgres public | groups_id_seq | sequence | postgres
テーブル定義を確認する
db_name=> \d groups Table "public.groups" Column | Type | Modifiers ------------+-----------------------------+----------------------------------------------------- id | integer | not null default nextval('groups_id_seq'::regclass) group_name | character varying | deleted_at | timestamp without time zone | created_at | timestamp without time zone | not null updated_at | timestamp without time zone | not null Indexes: "groups_pkey" PRIMARY KEY, btree (id)
SQL文の実行
SQLをそのまま打ち込み、実行可能。当たり前かw
例 該当するテーブルを削除
db_name=> drop table groups;