Switching Databases From MySQL to PostgreSQL 8.2Scott Fehrman, October 2007 Contents:
I've been a casual user of MySQL. Now that the Solaris 10 8/07 OS has been officially released, with updated support for PostgreSQL, I decided to give it a try and see what it took for a MySQL user to Get Started with PostgreSQL. Here are a few of the PostgreSQL features and enhancements in this release of Solaris 10:
See below for a list of the new PostgreSQL features and enhancements in this release of Solaris 10. The official Sun PostgreSQL web page has more information. Getting StartedBy default the PostgreSQL service is disabled: # svcs postgresql STATE STIME FMRI disabled Sep_06 svc:/application/database/postgresql:version_81 disabled Sep_06 svc:/application/database/postgresql:version_82 #
Notice: Before we go any further, note that there are two different instances of the
The Solaris 10 8/07 OS also has a preconfigured # 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 needs to be set up before we can use it. There are a few simple things we need to do:
Setting Up Your Environment (Version 8.2)
Initializing the Database (Version 8.2)
Before the PostgreSQL database can be started it needs a "first-time" initialization. Use the # 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
$
Notice: The output of the Starting the Database Service (Version 8.2)
The PostgreSQL database is ready to be started. Use the # 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 will automatically start the PostgreSQL database when the system is booted. Using the Database (Version 8.2)The main PostgreSQL command for interactive, command-line, operations is
$ /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
To enter the interactive terminal mode, use the $ /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=#
The Create a table: 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 Describe the table: postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | location | table | postgres
(1 row)
Populate the table: 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
Select the table: 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) Drop the table: postgres=# drop table location; DROP TABLE Stopping the Database (Version 8.2)
If you want to stop the PostgreSQL database, use the Solaris Service Management Facility # 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 Documentation
Many manual pages provide lots of information. I'd suggest starting with the
The PostgreSQL web site has a nice set of online documentation: Using Admin Tool
For those who want a Graphical User Interface (GUI), # gunzip pgadmin3-1.6.3_x86.tar.gz # tar xf pgadmin3-1.6.3_x86.tar # pkgadd -d . pgadmin3 # /opt/bin/pgadmin3 Converting SQL Scripts From MySQL to PostgreSQL
I have a few projects that were using MySQL. I needed to have the same
functionality in PostgreSQL. Unfortunately these scripts did not work
without some minor modifications. Here is what I changed, relative to
the
SummaryThe conversion from MySQL to PostgreSQL was easy. I didn't have to:
PostgreSQL Features and Enhancements in Solaris 10 8/07 OSHere's an overview of the new PostgreSQL features and enhancements in this release of Solaris 10:
More InformationHere are additional resources:
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 | |||||||||||||||||||||||||||||||||||