Now you have everything you need to run a database server!
In case the database gets curropted for some reason and you
want to start afresh, you can always run the script again.
You need to start the server every time you log in to a
machine and stop it before you log out. PostgreSQL comes with
a utility called "pg_ctl
" which allows for
safe starting and stopping of the server. Run the following
command to start the server.
$ pg_ctl start -o -i
Trouble shooting:
If you got the following complains
LOG: could not bind IPv4 socket: Address already in use
HINT: Is another postmaster already running on port 8888?
1) you need to kill the zombie postermasters
(find the process ids by "ps -U user-id", then "kill -9 processid")
2) and rm the lock from /tmp/ (e.g. .s.PGSQL.4151 and
.s.PGSQL.4151.lock),
3) or check if someone else is using that port, just change the PGPORT
to other number (i.e., ls -la /tmp/ see if there are other locks on the
port you set).
The option -o
passes the quoted text
to the server, which in this case the -i
option tells the server to accept connections via TCP/IP. We recommend
using
$ pg_ctl start -s
-l $PGDATA/serverlog -o '-i'
to start the server.
psql
which enables interactive processing of SQL commands.
$ psql [database]
Notice that the parameter to "psql" is the name of database on the server. If you do not specify a database, "psql" will attempt to connect to the database with the same name as your login (created for you by setup_db.sh). There are two ways to input queries. You can directly input SQL commands in the command line or you can use files.
Once "psql"
is started, you can enter any SQL command. Commands can be multi-line
and MUST have a semicolon at the end. "psql"
has its own prompt, which includes the database name and either a
equals sign or hyphen followed by a pound sign. The hyphen tells you
that a partial SQL statement has been entered. For example,
test=# CREATE TABLE students
test-#
(id int, firstname char(15), lastname char(15), email
char(30));
Notice that after the first line, the prompt changed. This is
particularly helpful when you enter a multi-line statement.
The above SQL command will create a table named "student
"
with four fields.
Typing SQL directly into "psql" can be error-prone and leaves no log of the commands you ran. You may want to put a number of SQL commands into a file and then have "psql" run each of those commands. The file can contain multi-lined statements along with as much white space as you want. Once the file is ready, from within psql you can execute the file. For example:
test=# \i query.sql
The file "query.sql
" may contain the
following SQL commands:
$ more query.sql
INSERT
INTO students VALUES
(1950009,
'Joe', 'Smith', 'jsmith@andrew.cmu.edu');
SELECT * FROM students;
The above file will insert a row to the "students
"
table and show the content of the table.
Apart from SQL, psql recognizes few other useful commands. You already know about \i, the following list describes a few other commonly used commands.
\d
or \d table_name
Please read the documentation for "psql" at: http://www.postgresql.org/docs/8.3/static/app-psql.html. We highly recommend at least skimming the online documentation.
PostgreSQL will compute the entire answer BEFORE it shows any results. When running long queries, it may appear as though the server has frozen, but it may just be working. You can stop a long running query by pressing ^C.
NOTE: In your SQL queries, you need to put
table names (or attribute names) in double-quotes if they include
capital letters. Otherwise you'll get an error from PostgreSQL that it
doesn't recognize the table (attribute).
Stopping the server also uses the "pg_ctl" utility. Type the command:
$ pg_ctl stop
Please
stop the server before you log out from the system completely. If you
keep forgetting to shutdown, please copy ~/../binf415/bash_logout to
~/.bash_logout. This will ensure automatic shutdown on logout.
$ createdb test
$ dropdb test
PostgreSQL maintains its own database of users. By default the
only user created is the same as the UNIX login of the person who ran
the initdb
command (i.e. your account). This
account has all permissions. The account is not created with a
password, so you should assign one. At the "psql" prompt
type: (Replace the
userid and password with your own login id and desired password.)
test=# ALTER USER userid WITH PASSWORD
'password';
ALTER USER
" After you've changed
the password, you can exit "psql
". Depending
on the server configuration, password will be stored in clear-text or
encrypted text. In our experimental environment, it will be stored in
encryped text.
$PGDATA/pg_hba.conf
"
contains the configuration options for security. The file contains lots
of comments describing the various options for the interested reader.
We'll suggest a moderate policy. The end of the file (lines 84-88)
contains the actual configuration. Please change the method from
"trust" to "md5" at line 84, line 86, and line 48. After the change, anyone
attempting to connect to the database (either by local UNIX or TCP/IP
socket) will be required to specify a username and password. By
default, "psql
" will use your UNIX login as
the username and then prompt for the password.
If you have altered your password by issuing the "ALTER
USER username WITH UNENCRYPTED PASSWORD 'password'"
"
command, the password will be stored in clear-text and you need to use
"password" instead of "md5" in the METHOD field.