一、安裝postgresql
yum -y install postgresql postgresql-server postgresql-devel
mkdir /work/pgdata
useradd postgres -d /work/pgdata
chown postgres.postgres -R /work/pgdata
su - postgres
vim ~/.bash_profile 添加如下內容
export PGHOME=/work/pgdata
export PGDATA=/work/pgdata/data
二、初始化並設定postgresql字元集
1、修改系統字元集為:UTF-8
vim /etc/sysconfig/i18n
LANG="en_US.UTF-8"
2、切換到postgres帳號,初始化資料庫
initdb -E UTF8
3、修改postgresql設定檔
/work/pgdata/data/postgresql.conf修改
listen_addresses = '*'
port = 5432
max_connections = 1000
/work/pgdata/data/pg_hba.conf 最後添加可信任網路:
1
2
host all all 10.0.0.1/8 trust
host all all 192.168.0.0/24 trust
三、啟動資料庫
/etc/init.d/postgresql start
chkconfig postgresql on
ps aux | grep postgresql
postgres 41659 0.0 0.0 216280 5840 ? S 16:33 0:00 /usr/bin/postmaster -p 5432 -D /work/pgdata/data
postgres 41661 0.0 0.0 179292 1284 ? Ss 16:33 0:00 postgres: logger process
postgres 41663 0.0 0.0 216280 1764 ? Ss 16:33 0:00 postgres: writer process
postgres 41664 0.0 0.0 216280 1472 ? Ss 16:33 0:00 postgres: wal writer process
postgres 41665 0.0 0.0 216548 1808 ? Ss 16:33 0:00 postgres: autovacuum launcher process
postgres 41666 0.0 0.0 179420 1528 ? Ss 16:33 0:00 postgres: stats collector process
postgres 41686 0.0 0.0 218248 5428 ? Ss 16:35 0:00 postgres: opcode opcode 192.168.0.80(38453) idle
postgres 41832 0.0 0.0 217636 4892 ? Ss 16:54 0:00 postgres: postgres postgres [local] idle
四、建立資料庫和對應帳號:
su - postgres
系統提示符會變為”postgres=#”,表示這時已經進入了資料庫控制台。
\password postgres; #給postgres設定密碼
CREATE USER opcode WITH PASSWORD 'opcodepasswd';
CREATE DATABASE opcode OWNER opcode;
GRANT ALL PRIVILEGES ON DATABASE opcode to opcode;
\h:查看SQL命令的解釋,比如\h select。
\?:查看psql命令列表。\l:列出所有資料庫。
\c [database_name]:串連其他資料庫。
\d:列出當前資料庫的所有表格。
\d [table_name]:列出某一張表格的結構。
\du:列出所有使用者。
\e:開啟文字編輯器。
\conninfo:列出當前資料庫和串連的資訊。
#其他知識
createuser --superuser postgres #建立並設定管理員帳號