PostgreSQL & PHP Tutorials - PostgreSQL Databases

PostgreSQL »  PostgreSQL Databases
PostgreSQL »  Starting Out »  PostgreSQL Databases

Posted By Chris Smith Posted on 07 Feb 2006, 09:22 PM
PostgreSQL of course supports multiple databases. Here's a quick look at creating them and making sure the right user owns them.

The command is 'createdb':

$ /usr/local/pgsql/bin/createdb --help

The most important part is the owner of the database. This allows that user total access to that data (if we give another user access to the data we have to grant access to each & every part of it, so creating the database properly in the first place is quite important).

The first php project we'll look at is creating a simple contact list.

First of all, we'll need to create a user:

$ /usr/local/pgsql/bin/createuser -P contacts

I used the password 'firstphp'.

The new user should not be able to create another database or another user - so enter 'n' at both of these prompts.

Now we have the user, we can create the database:

$ /usr/local/pgsql/bin/createdb --owner=contacts contacts

It will prompt you for YOUR password (not the owner's password), so enter that and away you go.

If we list the databases now:
$ /usr/local/pgsql/bin/psql -l

we see:

List of databases
Name | Owner | Encoding
-----------+----------+-----------
contacts | contacts | SQL_ASCII
template0 | postgres | SQL_ASCII
template1 | postgres | SQL_ASCII
(3 rows)

So the 'contacts' database has the right owner, which is important later.

If you need to, you can drop or delete the database. The command is similar to creating it:

$ /usr/local/pgsql/bin/dropdb


So we've covered server authentication and creating users and databases in PostgreSQL, we're off to a good start!
Avg Rating: 4
Vote Count: 4


              

  New Reply


Well written.

Systematic approach

Thanks for this great effort!
Biju GK 21 Mar 2006 Reply


Want to post a comment? Fill in the details below.

Your Name  : 
Your Email  : 
Your Website  : 
Spam Check! Please answer this question  : 9 + 5 =
Comment