將資料庫由 MySQL 切換為 PostgreSQL 8.2Scott Fehrman (2007 年 10 月) 目錄:
我偶爾會使用 MySQL。既然現在已經正式發行 Solaris 10 8/07 作業系統,其中含有 PostgreSQL 的最新支援,我決定給它一次機會,看看 MySQL 使用者如何著手使用 PostgreSQL。 以下是這一版 Solaris 10 中部份的 PostgreSQL 特性和增強功能:
這一版 Solaris 10 中新的 PostgreSQL 特性和增強功能清單如下所示。Sun 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 作業系統也有預先配置的 # 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
....
$
必須先設定 PostgreSQL,才可加以使用。我們需要執行幾項簡單的作業:
設定環境 (8.2 版)
初始化資料庫 (8.2 版)
PostgreSQL 資料庫需要「首次」初始化,才能予以啟動。使用 # 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
$
注意: 啟動資料庫服務 (8.2 版)
PostgreSQL 資料庫已準備進行啟動。使用 # 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 服務管理功能將自動啟動 PostgreSQL 資料庫。 使用資料庫 (8.2 版)用於互動式指令行操作的主要 PostgreSQL 指令是
$ /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
若要進入互動式終端機模式,請使用 $ /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=# 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 服務管理功能的 # 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 文件
許多手冊都提供大量資訊。我建議由
PostgreSQL 網站上有一組實用的線上文件: 使用管理工具
對於想要使用圖形化使用者介面 (GUI) 的使用者而言, # gunzip pgadmin3-1.6.3_x86.tar.gz # tar xf pgadmin3-1.6.3_x86.tar # pkgadd -d . pgadmin3 # /opt/bin/pgadmin3 將 SQL 程序檔從 MySQL 轉換成 PostgreSQL
我有幾個使用 MySQL 的專案。我需要在 PostgreSQL 中有相同的功能。不幸地,這些程序檔不做些小修改就無法使用。以下是我所做的變更,這與
摘要從 MySQL 轉換為 PostgreSQL 很容易。我不需要:
Solaris 10 8/07 作業系統中的 PostgreSQL 特性和增強功能以下是這一版 Solaris 10 中新的 PostgreSQL 特性和增強功能的簡介:
更多資訊以下是其他相關資源:
Comments (latest comments first)Discuss and comment on this resource in the BigAdmin Wiki
Unless otherwise licensed, code in all technical manuals herein (including articles, FAQs, samples) is provided under this License. |
BigAdmin SubscriptionsBigAdmin Areas
BigAdmin Sun Center
BigAdmin Topics | |||||||||||||||||||||||||||||||||||