postgres

これが学習対象の本命
  • インストール
root# yum install postgres*
  • 初期設定
root# su postgres
postgres$ cd
postgres$ source .bash_profile
postgres$ initdb --encoding=UTF8
postgres$ pg_ctl start
postgres$ exit
root# /etc/init.d/postgresql start
root# chkconfig postgresql on

  • データベースの作成
root# su - postgres
postgres$ createdb redmine

  • ユーザの作成
postgres$ createuser -P

Enter name of role to add: redmine
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE

  • テーブルを作る
create table testm (
 key            char(008)     primary key,
 data1          int8,
 data2          int8,
 data3          int8
);


  • テーブルを確認する
root# psql -h localhost -U postgres
postgres=# select * from pg_tables where not tablename like 'pg%' order by tablename;

  • SQL覚書
CREATE USER hoge WITH PASSWORD 'foo';

INSERT into products (title, description, image_url, price) values ('hoge', 'foo', 'url', '100');
select * from products;
UPDATE products SET price=200 where id=5;

DELETE FROM products where price=100;

<カラムを追加する>
ALTER TABLE <テーブル名> ADD <カラム名> <型情報>;

型:text varchar(n) int char(n) timestamp

<テーブルのオーナーを変更する>
ALTER TABLE テーブル名 OWNER TO ユーザ名;

<カラムの型を確認する>
\d <テーブル名>

SELECT
       pg_attribute.attname,
       pg_type.typname,
       pg_attribute.attlen
FROM
       pg_attribute,
       pg_type
WHERE
       pg_attribute.atttypid = pg_type.oid AND
       ( pg_attribute.atttypid < 26 OR pg_attribute.atttypid > 29 ) AND
       attrelid IN (
               SELECT
                       pg_class.oid
               FROM
                       pg_class,
                       pg_namespace
               WHERE
                       relname='usertbl' AND
                       pg_class.relnamespace=pg_namespace.oid    
       );



最終更新:2008年12月10日 16:39