xiaop@localhost$ sudo apt-get install postgresql-8.2
xiaop@localhost# pkginstall post*.tgz
或
xiaop@localhost# slapt-get --install postgresql-8.2.4
xiaop@localhost~# /etc/init.d/postgresql-8.2 start 注:启动;
xiaop@localhost~# /etc/init.d/postgresql-8.2 restart 注:重启;
xiaop@localhost~# /etc/init.d/postgresql-8.2 stop 注:停止;
xiaop@localhost~# /etc/init.d/postgresql-8.2 status 注:查看状态;
xiaop@localhost~# /etc/rc.d/rc.postgres start
xiaop@localhost~$ createuser testuser
Shall the new user be allowed to create databases? (y/n) n --------是否可以创建数据库:否
Shall the new user be allowed to create more new users? (y/n) n ---------是否可以创建新用户:否
CREATE USER
xiaop@localhost~$ createuser -h 172.28.18.51 -p 5000 -D -A -e testuser
CREATE USER joe NOCREATEDB NOCREATEUSER;
CREATE USER
xiaop@localhost~$ createuser -P -d -a -e testuser
Enter password for new user: testuser
Enter it again: testuser
CREATE USER joe PASSWORD 'testuser' CREATEDB CREATEUSER;
CREATE USER
xiaop@localhost~$ dropuser testuser
DROP USER
xiaop@localhost~$ dropuser -p 5000 -h 172.28.18.51 -i -e testuser
User "testuser" and any owned databases will be permanently deleted.
Are you sure? (y/n) y
DROP USER "testuser"
DROP USER
xiaop@localhost~$ createdb mydb
CREATE DATABASE
createdb: command not found
xiaop@localhost~$ createdb
xiaop@localhost~$ dropdb mydb
xiaop@localhost~$ psql mydb
Welcome to psql 8.2.4, the PostgreSQL interactive terminal.
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
mydb=#
mydb=> \h
mydb=> \q
xiaop@xiaop-laptop:~$ sudo apt-get install pgadmin3
xiaop@xiaop-laptop:~$ /usr/bin/pgadmin3 start
地址:localhost
描述:服务器名称(随意填写)
维护数据库:postgres
用户名:自己创建一个(详情参见创建用户)
密码:和用户名对应(创建用户时自己创建)
mydb#CREATE TABLE weather (
city varchar(80),
temp_lo int, -- 最低气温
temp_hi int, -- 最高气温
prcp real, -- 降水量
date date
);
mydb#DROP TABLE tablename
mydb#INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
mydb#INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
mydb#COPY weather FROM '/home/user/weather.txt';
SELECT * FROM weather;
<code>
输出结果:
<code>
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 43 | 57 | 0 | 1994-11-29
Hayward | 37 | 54 | | 1994-11-29
(3 rows)
SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
city | temp_avg | date
---------------+----------+------------
San Francisco | 48 | 1994-11-27
San Francisco | 50 | 1994-11-29
Hayward | 45 | 1994-11-29
(3 rows)
mydb#SELECT * FROM weather
WHERE city = 'San Francisco' AND prcp > 0.0;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
(1 row)
mydb#SELECT * FROM weather
ORDER BY city;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
Hayward | 37 | 54 | | 1994-11-29
San Francisco | 43 | 57 | 0 | 1994-11-29
San Francisco | 46 | 50 | 0.25 | 1994-11-27
SELECT * FROM weather
ORDER BY city, temp_lo;
mydb#SELECT DISTINCT city
FROM weather;
city
---------------
Hayward
San Francisco
(2 rows)
mydb#CREATE VIEW myview AS
SELECT city, temp_lo, temp_hi, prcp, date, location
FROM weather, cities
WHERE city = name;
SELECT * FROM myview;
city | temp_lo | temp_hi | prcp | date | location
---------------+---------+---------+------+------------+-----------
San Francisco | 46 | 50 | 0.25 | 1994-11-27 | (-194,53)
San Francisco | 43 | 57 | 0 | 1994-11-29 | (-194,53)
(2 rows)
mydb#UPDATE weather
SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2
WHERE date > '1994-11-28';
SELECT * FROM weather;
city | temp_lo | temp_hi | prcp | date
---------------+---------+---------+------+------------
San Francisco | 46 | 50 | 0.25 | 1994-11-27
San Francisco | 41 | 55 | 0 | 1994-11-29
Hayward | 35 | 52 | | 1994-11-29
(3 rows)
mydb#DELETE FROM weather WHERE city = 'Hayward';
DELETE FROM tablename;
0人
|
了这篇文章 |
点击图片可刷新验证码请点击后输入验证码博客过2级,无需填写验证码
同时赞一个