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
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).
-o passes the quoted text
to the server, which in this case the
option tells the server to accept connections via TCP/IP. We recommend
$ pg_ctl start -s
-l $PGDATA/serverlog -o '-i'
to start the server.
psqlwhich 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.
is started, you can enter any SQL command. Commands can be multi-line
and MUST have a semicolon at the end.
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 "
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', 'email@example.com');
SELECT * FROM students;
The above file will insert a row to the "
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.
Please read the documentation for "psql" at: http://www.postgresql.org/docs/8.2/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
stop the server before you log out from the system completely. If you
keep forgetting to shutdown, please copy ~badityap415/bash_logout to
~/.bash_logout. This will ensure automatic shutdown on logout.
You should see a line CREATE DATABASE indicating the request was
successful. The opposite request:
$ createdb test
will remove the "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
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.)
If the command is successful, "psql"
will display "
test=# ALTER USER userid WITH 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 47-48) contains the actual configuration. Please change the method from "trust" to "md5" at line 47 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 "
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.