Creating Users and User Databases
Now the system knows about PostgreSQL, so we can start creating PostgreSQL users, and user databases. There is a certain hierarchy to a Database site—this is necessary lingo. The Postmaster is the process that manages connections, how data is stored, and so forth. It is the parent process that manages the database server processes. It can run multiple databases underneath it, while at the same time multiple Postmasters can be running on the same machine. Most setups usually have a database per project or user, and the user puts her tables in the database.
Adding the proper paths to your default login file saves a lot of headaches. Most new Linux users and some FreeBSD users (including myself) are using BASH, so I will outline the steps for this. Simply add these lines to your .bashrc, .bash_login, or .bash_profile files.
PATH=$PATH:/usr/local/pgsql/bin # this tells the shell where to look for applications MANPATH=$MANPATH:/usr/local/pgsql/man # this tells the shell where to look for those handy documents PGLIB=/usr/local/pgsql/lib # this tells the shell and Postgres where to look for shared libs PGDATA=/usr/local/pgsql/data # this is the default directory where data is stored export PATH MANPATH PGLIB PGDATA # this tells the shell to pay attention to all the changes you have made.
There are two ways you can make the shell load up your changes: logout and then log back in, or
euphoria:~$ source .bashrc
Beginners often omit (or possibly overlook) the following step. This command is necessary:
Now it is time to start PostgreSQL. This step will normally be done by the system at startup time, but for testing purposes we will run it from the command line.
euphoria:~$ postmaster -i &
The -i tag allows connections over the Internet. Who can connect (and how) is described in the /usr/local/pgsql/data/pg_hba.conf file. Postmaster does not have to be restarted when this file is changed. For example, to allow all users to access all databases on the local host, include this line:
host all 127.0.0.1 255.255.255.255 trust
If you would like to require your local subnet, e.g. 192.168.0.0/24, to connect using a password use a line similar to this:
host all 192.168.0.0 255.255.255.0 password
Now we can finally create a postgres database user—this process can be repeated for as many users as you like. You will be asked a few simple questions.
Enter name of user to add ---> bob Enter user's postgres ID -> 65555 Is user "bob" allowed to create databases (y/n) n Is user "bob" a superuser? (y/n) n createuser: bob was successfully added Shall I create a database for "bob" (y/n) y
Please note that bob does not need a Unix account. If this is the case, give him a userid above 65536 (2^16 is the maximum userid for Unix).
For the PostgreSQL user, you will want to give her all the permissions so they can create databases for other users.
euphoria:~$ createuser postgres Enter user's postgres ID or RETURN to use Unix user ID: 1005 -> Is user "postgres" allowed to create databases (y/n) y Is user "postgres" a superuser? (y/n) y createuser: postgres was successfully added
Finally, we can connect to the PostgreSQL database using the psql command to enter the PostgreSQL Interactive Monitor.
euphoria:~$ psql Welcome to the POSTGRESQL interactive sql monitor: Please read the file COPYRIGHT for copyright terms of POSTGRESQL [PostgreSQL 7.0 on i386-unknown-freebsd3.4, compiled by cc ] type \? for help on slash commands type \q to quit type \g or terminate with semicolon to execute query You are currently connected to the database: postgres postgres=>
Now you can begin playing with SQL and PostgreSQL. There are many SQL tutorials on the Internet, and this article will be followed with others that relate directly to PostgreSQL and its interfaces.
About the Author: Jeff MacDonald is an avid FreeBSD user from Nova Scotia, Canada. He is employed by hub.org networking services, and is a partial owner of PostgreSQL, Inc. In his spare time he enjoys mountain biking and skydiving, and he is also a volunteer fire fighter. (In short, he is an Adrenaline Addicted Geek!)