[ Home ]

Cover of SQL Cover of SQL Tricks Cover of SQL Short Course Cover of SQL Visual QuickStart Guide 3rd Edition

Creating the Sample Database in PostgreSQL

To create books in PostgreSQL:

  1. 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 -W option or press Enter at the password prompt). PostgreSQL creates a new, empty database named books.

    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 to localhost, set user to postgres, and use the password that you assigned to postgres when you set up PostgreSQL.

  2. At the command prompt, type:

    psql -h host -U user -W -f books_postgresql.sql books

    The -f option specifies the name of the SQL file. You can include an absolute or relative pathname (see the “Paths” section in “Running SQL Programs” in Chapter 1 of SQL or SQL Tricks, or in Appendix B of SQL Short Course or SQL Crash Course for Beginners).

    psql displays the results. Ignore the messages about nonexistent tables — they’re caused by the script’s DROP TABLE statements, which you’ll need to rerun books_postgresql.sql later to restore the tables to their original states.

    psql
  3. To run SQL scripts and interactive statements against books, see “PostgreSQL” in Chapter 1 of SQL or SQL Tricks, or in Appendix B of SQL Short Course or SQL Crash Course for Beginners.

Tips