|
SQL: Visual QuickStart Guide, Third EditionCreating the Sample Database - PostgreSQL |
To create books in PostgreSQL:
-
At a command prompt, type:
createdb -h host -U user -W books
host is the host name, and user is your PostgreSQL user name. PostgreSQL will prompt you for your password (for a passwordless user, either omit the-Woption or press Enter at the password prompt). PostgreSQL creates a new, empty database namedbooks.
If PostgreSQL is running on a remote network computer, ask your database administrator (DBA) for the connection parameters. If you're running PostgreSQL locally (that is, on your own computer), then set host tolocalhost, set user topostgres, and use the password that you assigned topostgreswhen you set up PostgreSQL.
-
At the command prompt, type:
psql -h host -U user -W -f books_postgresql.sql books
The-foption specifies the name of the SQL file. You can include an absolute or relative pathname (see the "Pathnames" sidebar in "Running SQL Programs" in Chapter 1).
psqldisplays the results. Ignore the messages about nonexistent tables — they're caused by the script'sDROPTABLEstatements, which you'll need to rerunbooks_postgresql.sqllater to restore the tables to their original states.
-
To run SQL scripts and interactive statements against
books, see "PostgreSQL" in Chapter 1.
Tips
-
You can set the environment variables
PGDATABASEandPGUSERto specify the default database and the user name used to connect to the database. See "Environment Variables" in the PostgreSQL documentation.
-
As an alternative to the command prompt, you can use the pgAdmin graphical tool. If the PostgreSQL installer didn't install pgAdmin automatically, you can download it for free at http://pgadmin.org.
-
The script
books_postgresql.sqlis identical to the standard SQL scriptbooks_standard.sql.