Interfacing Relational Databases to the Web
PostgreSQL uses a different access system than the rest of your system; oddly enough, not even root has access to the database by default. The database system has its own user system and passwords, and postgres is the database administrator account by default. The advantage to the separate access system is that one can create database users who do not have UNIX accounts; this way, the database for your web application can specify access control without creating a potential security hole for your system. To add your web administrator (web) as a database user, use createuser (as root):
# su postgres -c createuser Enter name of user to add ---> web Enter user's postgres ID, or RETURN to use UNIX user id: 542 -> 542 Is user "web" allowed to create databases (y/n) y Is user "web" allowed to add users? (y/n) y createuser: web was successfully added
Then, as web (or whatever account you used), you'll be able to create a database with createdb foo and then try some queries on foo using psql foo.
You'll also need to set up PostgreSQL to accept incoming TCP/IP connections so your PHP3 pages can access it. Fortunately, System V init makes this easy. Simply open the file /etc/rc.d/init.d/postgresql and change the line
su postgres -l -c \ 'usr/bin/postmaster -S -D/var/lib/pgsql'
so that it reads
su postgres -l -c \ '/usr/bin/postmaster -S -D/var/lib/pgsql -i'While you're at it, you will probably want to specify a different port from the default (5432) for security reasons. To run the PostgreSQL back end on a different port, merely append a -p port to the above line.
Just about every relational database in the world uses SQL (or some extended version of SQL) as its query language. SQL allows you to define tables, select records based on given criteria, update values in one or many records and delete records. This is just a brief introduction to SQL; for more complete references, see Resources.
To create a table, one uses the CREATE TABLE statement. Its syntax is as follows:
CREATE TABLE tablename (field-1 type-1, ..., field-n type-n)
In psql, you will need to end each statement with a semicolon. These semicolons are not part of the SQL language, but rather for the benefit of psql's lexer.
You may also declare fields as NOT NULL, UNIQUE or PRIMARY KEY, or specify a value as DEFAULT to a field. PostgreSQL will create an index on primary key fields. Unfortunately, as of version 6.4, PostgreSQL does not support foreign keys, but at least the parser will not choke on the SQL REFERENCES keyword.
Here's an example, akin to the UNIX password file:
CREATE TABLE passwd (username varchar(8) PRIMARY KEY, -- PRIMARY KEY implies UNIQUE cryptedpass char(13), uid int UNIQUE NOT NULL, gid int NOT NULL, gecos varchar(80), -- the GECOS field (real name, office, etc.) homedir varchar(80), shell varchar(50) DEFAULT '/bin/sh');
Note that SQL uses a double-dash to begin comments, which are terminated by a newline.
To insert data into a table, use the INSERT statement:
INSERT INTO tablename (field-1, ..., field-n) VALUES (value-1, ..., value-n/)
You needn't specify field names if you are inserting values into every field. Here's an example for the table we just created:
INSERT INTO passwd (username, cryptedpass, uid, gid, gecos, homedir, shell) VALUES ('fred', '37MniLTaiPLaL', 42, 500, 'Fred Mbogo', '/home/fred/', '/bin/sh');Note that SQL uses single quotes for string constants. Any closet Pascal programmers will feel right at home.
The SQL SELECT statement returns records where values meet a certain criteria. Here are some examples of SELECT in action:
SELECT * FROM passwd; -- returns all fields of all records SELECT username FROM passwd; -- returns all usernames SELECT * FROM passwd WHERE username = 'fred'; SELECT * FROM passwd ORDER BY username, shell; SELECT * FROM passwd WHERE homedir LIKE '/home%' -- % is the SQL wildcard character AND shell = '/bin/sh' ORDER BY username; SELECT homedir, projectname FROM passwd, projects -- assuming we have a projects table WHERE -- this will return the home directory of passwd.username = projects.leader; -- each project leader for each project
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- RSS Feeds
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- Tech Tip: Really Simple HTTP Server with Python
- Connecting Android device to desktop Linux via USB
25 min 35 sec ago
- Find new cell phone and tablet pc
1 hour 23 min ago
2 hours 52 min ago
- Automatically updating Guest Additions
4 hours 1 min ago
- I like your topic on android
4 hours 47 min ago
- Reply to comment | Linux Journal
5 hours 8 min ago
- This is the easiest tutorial
11 hours 23 min ago
- Ahh, the Koolaid.
17 hours 1 min ago
- git-annex assistant
23 hours 1 min ago
- direct cable connection
23 hours 23 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.