| ||||||||||||||||||
| ||||||||||||||||||
| ||||||||||||||||||
PostgreSQL NotesIndexGeneral informationThe version of PostgreSQL we have installed is 9.1. Complete manuals are available from the PostgreSQL site (link). The most useful sections to this class are probably "Tutorial" and "The SQL Language". Command-line processorInteractive modeAfter logging in, at a shell prompt, you can start the command-line processor in the interactive mode by entering the following command: psqlWhen you are inside the command-line processor, you will get a prompt that looks like the following (where USER is your account name): USER=#You can now use any SQL commands (CREATE, DROP, SELECT, INSERT, DELETE) to manipulate the database. Remember to use semicolon (";") to terminate SQL statements. Inside psql, you will also find several (non-SQL) psql commands useful (these do not need to be terminated by semicolon):
The command-line processor provides advanced command-line editing and auto-completion support similar to what you would find in popular interactive shells such as bash. Note: By default, you will be automatically connected to the server running on the local machine as USER. In particular, you will be connected to a database named USER with default schema named USER. Hence, you do not need to worry about interfering with other users on the machine. To learn about other options available to you (including, for example, working with different databases managed by the database server), type man psql at your shell prompt for the manual. Immediate modeYou can use psql with the -c option to execute a psql command or SQL statement directly from your shell; e.g.: psql -c "\d" psql -c "select * from beer"After each command/statement completes, control is immediately returned back to your shell. Note that you do not need to end the SQL statements with ; in this case. You may need to enclose command/statement strings in quotes to prevent the shell from interpreting them (for example, the shell would expand * above into a list of files in your current directory, if you do not quote *). This method of invoking psql may be useful when writing shell scripts. Sometimes you might want to use an extra option -a to have psql echo the command/statement back, which is helpful in debugging the output from scripts. Batch modeYou can write multiple psql commands and SQL statements in one text file (say, named FILE), and then use the command psql -af FILE to execute them all. Use ";" to signal the end of each SQL statement in the file. You will see examples of this usage in /opt/dbcourse/examples/db-beers/setup.sh, which executes files create.sql, load.sql, and drop.sql in the batch mode. Loading a databaseYou can find an example of loading a database from data files in /opt/dbcourse/examples/db-beers/. First, you need to prepare the data files. The example data files in data subdirectory use a delimited ASCII format. Each line of the file represents a row, with columns of the row separated by commas. Then, use the \COPY command to load tables from data (see load.sql for syntax). Remember to check any errors reported. What if some of the data values contain commas? You should use a different column delimiter, e.g., |. And what if a column has no value and you want to load as having NULL instead of an empty string? The appropriate command would be "\COPY TABLE_NAME FROM 'DATA_FILE' WITH DELIMITER ',' NULL '' CSV". |
||||||||||||||||||
Last updated Wed Sep 11 10:18:07 EDT 2013 |