?
?
?
1 安装postgresql
--使用apt 直接安装:
dave@dave:~/cndba$ sudo apt-get installpostgresql postgresql-client postgresql-server-dev-all -y
?
--查看数据库状态:
postgres@dave:~$ /etc/init.d/postgresql status
Running clusters: 9.1/main
?
--停止:
postgres@dave:~$ /etc/init.d/postgresql stop
[ ok ] Stopping PostgreSQL 9.1 databaseserver: main.
?
--启动:
postgres@dave:~$ /etc/init.d/postgresql start
[ ok ] Starting PostgreSQL 9.1 databaseserver: main.
postgres@dave:~$
?
--查看进程:
postgres@dave:~$ ps -ef|grep postgres
root 9502 9184 0 06:34 pts/2 00:00:00 su - postgres
postgres 9510 9502 0 06:34 pts/2 00:00:00 -su
postgres 9869 1 0 06:52 ? 00:00:00/usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -cconfig_file=/etc/postgresql/9.1/main/postgresql.conf
postgres 9871 9869 0 06:52 ? 00:00:00 postgres: writer process
postgres 9872 9869 0 06:52 ? 00:00:00 postgres: wal writer process
postgres 9873 9869 0 06:52 ? 00:00:00 postgres: autovacuum launcherprocess
postgres 9874 9869 0 06:52 ? 00:00:00 postgres: stats collectorprocess
postgres 9905 9510 0 06:53 pts/2 00:00:00 ps -ef
postgres 9906 9510 0 06:53 pts/2 00:00:00 grep postgres
postgres@dave:~$
?
--设置开机自启动:
postgres@dave:~$ sudo update-rc.d postgresql start 88 2 3 4 5 . stop 88 0 1 6 .
update-rc.d: using dependency based bootsequencing
postgres@dave:~$
?
第一次安装后,默认生成一个名为postgres的数据库和一个名为postgres的数据库用户。同时还生成了一个名为postgres的Linux系统用户。
?
--修改postgres 用户密码:
dave@dave:~$ sudo passwd postgres
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
dave@dave:~$
?
2 查看数据库信息
--查看数据库信息:
dave@dave:~$ su - postgres
Password:
postgres@dave:~$ psql
psql (9.1.15)
Type "help" for help.
?
postgres=# help
You are using psql, the command-lineinterface to PostgreSQL.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
postgres=#
?
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
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 rows)
?
postgres-#
?
--连接到某个数据库:
postgres-# \c postgres
You are now connected to database"postgres" as user "postgres".
postgres-# \c template1
You are now connected to database"template1" as user "postgres".
template1-#
?
postgres=# \c postgres
You are now connected to database"postgres" as user "postgres".
?
--显示当前的数据库:
postgres=# select current_database();
current_database
------------------
postgres
(1 row)
?
--该命令显示当前的userid:
postgres=# select current_user;
current_user
--------------
postgres
(1 row)
?
?
--退出操作界面:
template1-# \q
postgres@dave:~$
?
?
3 创建DB对象
?
默认的postgres用户是超级管理员,权限太大,所以一般建议创建一个独立的管理用户。
?
dave@dave:~$ su - postgres
Password:
postgres@dave:~$ psql
psql (9.1.15)
Type "help" for help.
?
--创建数据库用户、数据库,并赋予新用户新数据库的全部权限:
postgres=# create user dave with password'dave';
CREATE ROLE
postgres=# create database cndba;
CREATE DATABASE
postgres=# grant all privileges on databasecndba to dave;
GRANT
postgres=#
?
p