solaris 10 启动 postgresql

 默认情况下,PostgreSQL 服务处于禁用状态:

# svcs postgresql
STATE          STIME    FMRI
disabled       Sep_06   svc:/application/database/postgresql:version_81
disabled       Sep_06   svc:/application/database/postgresql:version_82

Solaris 10 8/07 操作系统还有一个预配置的 postgres 用户,并且它使用 Solaris RBAC 功能确保该用户/角色可以管理 SMF postgresql:version_82 服务。

# grep postgres /etc/user_attr
postgres::::type=role;profiles=Postgres Administration,All
# su - postgres
$ id
uid=90(postgres) gid=90(postgres)
$ profiles
Postgres Administration
All
Basic Solaris User
$ profiles -l

Postgres Administration:
/usr/postgres/8.2/bin/initdb            uid=postgres
/usr/postgres/8.2/bin/ipcclean          uid=postgres
/usr/postgres/8.2/bin/pg_controldata    uid=postgres
/usr/postgres/8.2/bin/pg_ctl            uid=postgres
/usr/postgres/8.2/bin/pg_resetxlog      uid=postgres
/usr/postgres/8.2/bin/postgres          uid=postgres
/usr/postgres/8.2/bin/postmaster        uid=postgres

 

* 设置环境(主要用于简化命令)、PATH、MANPATH 和 PGDATA。
* 初始化数据库。
* 启动数据库服务。

设置环境(8.2 版)

* 更改 PATH

如上所述,Solaris 同时支持 PostgreSQL 8.1 和 8.2 版。如果不更改默认 PATH 变量,将使用 Postgres 命令(在 /usr/bin 中)。修改 PATH 并将 /usr/postgres/8.2/bin 置于任何其他路径名之前。您可以在用户的登录脚本、全局登录脚本(例如,/etc/profile)中设置 PATH,或者通过以下命令行对其进行设置:

# echo $PATH
/usr/sbin:/usr/bin
# PATH=/usr/postgres/8.2/bin:
# export PATH
# echo $PATH
/usr/postgres/8.2/bin:/usr/sbin:/usr/bin

* 更改 MANPATH

如上所述,Solaris 同时支持 PostgreSQL 8.1 和 8.2 版。如果不更改默认 MANPATH 变量,将使用 Postgres 手册页(在 /usr/man 中)。修改 MANPATH 并将 /usr/postgres/8.2/man 置于任何其他路径名之前。您可以在用户的登录脚本、全局登录脚本(例如,/etc/profile)中设置 MANPATH,或者通过以下命令行对其进行设置:

# echo $MANPATH
/usr/man:/usr/sfw/man
# MANPATH=/usr/postgres/8.2/man:
# export MANPATH
# echo $MANPATH
/usr/postgres/8.2/man:/usr/man:/usr/sfw/man
#

* 设置 PGDATA

PostgreSQL 数据库可以利用一个名为 PGDATA 的环境变量。许多 PostgreSQL 命令都使用该变量。如果使用 PGDATA,需要将其设置为 PostgreSQL 数据目录所在位置的全限定路径名。PostgreSQL 数据的默认位置是 /var/postgres/8.2/data。您可以将 PGDATA 设置为此位置或其他位置,然后便不必再为那些需要知道数据目录路径名的 PostgreSQL 命令使用 -D 选项。

$ PGDATA=/var/postgres/8.2/data
$ export PGDATA
$ echo $PGDATA
/var/postgres/8.2/data
$


初始化数据库(8.2 版)

启动 PostgreSQL 数据库之前,需要先对其进行“首次”初始化。使用 initdb 命令可初始化数据库。有关更多详细信息,请参见 initdb 手册页。请以 postgres 用户身份运行 initdb 命令。在以下示例中,我使用了 initdb 命令和 PostgreSQL 数据目录的全限定路径。如果您已设置 PATH 和 PGDATA 环境变量,则只需执行 $ initdb 即可:

# su - postgres
$ /usr/postgres/8.2/bin/initdb -D /var/postgres/8.2/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale C.

fixing permissions on existing directory /var/postgres/8.2/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
creating configuration files ... ok
creating template1 database in /var/postgres/8.2/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

/usr/postgres/8.2/bin/postgres -D /var/postgres/8.2/data
or
/usr/postgres/8.2/bin/pg_ctl -D /var/postgres/8.2/data -l logfile start

$

注意:initdb 命令的输出会指示您如何启动和停止数据库。您可以忽略这些命令。我们将使用 Solaris Service Management Facility 来管理 PostgreSQL 数据库。

启动数据库服务(8.2 版)

PostgreSQL 数据库已做好启动准备。请使用 svcs 和 svcadm 命令(以 postgres 用户身份)来启动、停止和监视服务:

# su - postgres

$ svcs postgresql
STATE          STIME    FMRI
disabled       Sep_06   svc:/application/database/postgresql:version_81
disabled       14:06:11 svc:/application/database/postgresql:version_82

$ svcadm -v enable -s postgresql:version_82
svc:/application/database/postgresql:version_82 enabled.

$ svcs -l postgresql:version_82
fmri         svc:/application/database/postgresql:version_82
name         PostgreSQL RDBMS
enabled      true
state        online
next_state   none
state_time   Fri Sep 14 14:23:49 2007
logfile      /var/svc/log/application-database-postgresql:version_82.log
restarter    svc:/system/svc/restarter:default
contract_id 379
dependency   require_all/none svc:/milestone/network:default (online)
dependency   require_all/none svc:/system/filesystem/local:default (online)

$

Solaris Service Management Facility 会在系统引导时自动启动 PostgreSQL 数据库。

使用数据库(8.2 版)

PostgreSQL 用于交互式命令行操作的主要命令是 psql。有关 psql 的详细信息,请参见手册页。以下是 --help 选项的输出(我使用了 psql 命令的全限定路径;如果您已正确设置 PATH,则可以只使用命令名):

$ /usr/postgres/8.2/bin/psql --help
This is psql 8.2.4, the PostgreSQL interactive terminal.

Usage:
psql [OPTIONS]... [DBNAME [USERNAME]]

General options:
-d DBNAME       specify database name to connect to (default: "postgres")
-c COMMAND      run only single command (SQL or internal) and exit
-f FILENAME     execute commands from file, then exit
-1 ("one")      execute command file as a single transaction
-l              list available databases, then exit
-v NAME=VALUE   set psql variable NAME to VALUE
-X              do not read startup file (~/.psqlrc)
--help          show this help, then exit
--version       output version information, then exit

Input and output options:
-a              echo all input from script
-e              echo commands sent to server
-E              display queries that internal commands generate
-q              run quietly (no messages, only query output)
-o FILENAME     send query results to file (or |pipe)
-n              disable enhanced command line editing (readline)
-s              single-step mode (confirm each query)
-S              single-line mode (end of line terminates SQL command)
-L FILENAME     send session log to file

Output format options:
-A              unaligned table output mode (-P format=unaligned)
-H              HTML table output mode (-P format=html)
-t              print rows only (-P tuples_only)
-T TEXT         set HTML table tag attributes (width, border) (-P tableattr=)
-x              turn on expanded table output (-P expanded)
-P VAR[=ARG]    set printing option VAR to ARG (see \pset command)
-F STRING       set field separator (default: "|") (-P fieldsep=)
-R STRING       set record separator (default: newline) (-P recordsep=)

Connection options:
-h HOSTNAME     database server host or socket directory (default: "local socket")
-p PORT         database server port (default: "5432")
-U NAME         database user name (default: "postgres")
-W              prompt for password (should happen automatically)

For more information, type "\?" (for internal commands) or "\help"
(for SQL commands) from within psql, or consult the psql section in
the PostgreSQL documentation.

Report bugs to .
$

要进入交互式终端模式,请不带任何选项执行 psql 命令:

$ /usr/postgres/8.2/bin/psql
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

postgres=#

postgres=# 提示符表示 PostgreSQL 已就绪,正在等待指令。Internet 上有大量 PostgreSQL 教程,因此我将不对 PostgreSQL 的用法做任何细致阐述。PostgreSQL 文档 Web 站点提供了一整套指南和参考文档。以下是几个用于测试一些基本操作的命令:

创建表:

postgres=# CREATE TABLE location
(
CODE        VARCHAR(8) NOT NULL PRIMARY KEY,
DESCRIPTION VARCHAR(32) NOT NULL
);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "location_pkey" for table "location"
CREATE TABLE

描述表:

postgres=# \dt
List of relations
Schema |   Name   | Type | Owner  
--------+----------+-------+----------
public | location | table | postgres
(1 row)

填充表:

postgres=# INSERT INTO location ( CODE, DESCRIPTION ) VALUES
('loc30','Austin, Texas'),
('loc31','New York, New York'),
('loc32','Chicago, Illinois'),
('loc33','Dallas, Texas'),
('loc34','San Jose, California'),
('loc35','Atlanta, Georgia'),
('loc99','Denver, Colorado');
INSERT 0 7

选择表:

postgres=# select * from location;
code |     description
-------+----------------------
loc30 | Austin, Texas
loc31 | New York, New York
loc32 | Chicago, Illinois
loc33 | Dallas, Texas
loc34 | San Jose, California
loc35 | Atlanta, Georgia
loc99 | Denver, Colorado
(7 rows)

删除表:

postgres=# drop table location;
DROP TABLE


停止数据库(8.2 版)

如果要停止 PostgreSQL 数据库,请使用 Solaris Service Management Facility 的 svcadm 命令:

# su - postgres
$ svcadm disable -s postgresql:version_82
$ svcs postgresql:version_82
STATE          STIME    FMRI
disabled       15:11:38 svc:/application/database/postgresql:version_82

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值