m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

centosでyumでpostgresqlをインストール2

ユーザー確認

postgresユーザーが作成されているので確認

 id postgres
uid=26(postgres) gid=26(postgres) 所属グループ=26(postgres)
$

パスワードの変更後にユーザーを切り替え

sudo passwd postgres
su - postgres

psqlでコンソールを開く

-bash-4.1$ psql
psql (9.3.5)
Type "help" for help.

postgres=# \q
-bash-4.1$
作業ユーザーの確認
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}

postgres=#

※「\du」で作業ユーザーを確認できる

ユーザーの作成
postgres=# create user vagrant createdb password 'password' login;
CREATE ROLE
postgres=#

※ユーザーはvagrant
※データベース作成可能
※パスワードは「password」
※ログイン可能

ostgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication | {}
 vagrant   | Create DB                                      | {}

postgres=#

※作業ユーザーを確認すると増えている

vagrantのユーザーへ戻ってデータベースを作成
createdb vagrantdb
ログインする
 psql vagrantdb
psql (9.3.5)
"help" でヘルプを表示します.

vagrantdb=>
データベースの削除
[vagrant@localhost ~]$ psql -l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |      アクセス権
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       |
 template0 | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 vagrantdb | vagrant  | UTF8             | en_US.UTF-8 | en_US.UTF-8       |
(4 行)

[vagrant@localhost ~]$ dropdb vagrantdb
[vagrant@localhost ~]$ psql -l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |      アクセス権
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       |
 template0 | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | en_US.UTF-8 | en_US.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
(3 行)

[vagrant@localhost ~]$
テーブル操作
vagrantdb=> create table sample1(
vagrantdb(>     name varchar(100)
vagrantdb(> );
CREATE TABLE
vagrantdb=> insert into sample1(name) values('aaa');
INSERT 0 1
vagrantdb=> insert into sample1(name) values('bbb');
INSERT 0 1
vagrantdb=> select * from sample1;
 name
------
 aaa
 bbb
(2 行)

vagrantdb=>

所感

普段使用していないので扱いには戸惑うかも
無料系のデータベースは使えたほうがいいので一応多少は使えるようにしておく