From 421ca2cf028eec8d6143312e9d3769ccf0a5be75 Mon Sep 17 00:00:00 2001 From: anthonydb Date: Wed, 21 Apr 2021 08:24:53 -0400 Subject: [PATCH 1/3] Chapter 18 psql commands updated --- Chapter_18/psql_commands.txt | 37 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Chapter_18/psql_commands.txt b/Chapter_18/psql_commands.txt index 73ee260..fd00e7f 100644 --- a/Chapter_18/psql_commands.txt +++ b/Chapter_18/psql_commands.txt @@ -48,30 +48,36 @@ SELECT * FROM grades ORDER BY student_id, course_id; -- Listing 18-7: Importing data using \copy -DROP TABLE state_regions; - -CREATE TABLE state_regions ( - st varchar(2) CONSTRAINT st_key PRIMARY KEY, - region varchar(20) NOT NULL -); +DELETE FROM state_regions; \copy state_regions FROM 'C:\YourDirectory\state_regions.csv' WITH (FORMAT CSV, HEADER); --- Listing 18-8: Saving query output to a file + +-- Listing 18-8: + +DELETE FROM state_regions; + +psql -d analysis -U postgres -c "COPY state_regions FROM STDIN WITH (FORMAT CSV, HEADER);" < state_regions.csv + + +-- Listing 18-9: Saving query output to a file -- Enter psql settings -\a \f , \pset footer +\pset format csv -- This will be the query -SELECT * FROM grades; +SELECT * FROM grades ORDER BY student_id, course_id; -- Set psql to output results --- Note that Windows users must suppply forward slashes for +-- Note that Windows users must supply forward slashes for -- this command, which is opposite of normal use. \o 'C:/YourDirectory/query_output.csv' -- Run the query and output -SELECT * FROM grades; +SELECT * FROM grades ORDER BY student_id, course_id; + + +-- ADDITIONAL COMMAND LINE UTILITIES -- createdb: Create a database named box_office @@ -81,14 +87,15 @@ createdb -U postgres -e box_office -- Changing user and database name \c [database name] [user name] -\c gis_analysis postgres +\c box_office +\c box_office yourname -- Loading shapefiles into PostgreSQL -- For the US Census county shapefile in Chapter 14: -shp2pgsql -I -s 4269 -W Latin1 tl_2010_us_county10.shp us_counties_2010_shp | psql -d gis_analysis -U postgres +shp2pgsql -I -s 4269 -W Latin1 tl_2019_us_county.shp us_counties_2019_shp | psql -d analysis -U postgres -- For the Santa Fe roads and waterways shapfiles in Chapter 14: -shp2pgsql -I -s 4269 tl_2016_35049_roads.shp santafe_roads_2016 | psql -d gis_analysis -U postgres -shp2pgsql -I -s 4269 tl_2016_35049_linearwater.shp santafe_linearwater_2016 | psql -d gis_analysis -U postgres +shp2pgsql -I -s 4269 tl_2016_35049_roads.shp santafe_roads_2016 | psql -d analysis -U postgres +shp2pgsql -I -s 4269 tl_2016_35049_linearwater.shp santafe_linearwater_2016 | psql -d analysis -U postgres From c73941206cc58658e52604390292afc000ed626e Mon Sep 17 00:00:00 2001 From: anthonydb Date: Fri, 23 Apr 2021 08:57:16 -0400 Subject: [PATCH 2/3] Chapter 18 psql commands --- Chapter_18/psql_commands.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Chapter_18/psql_commands.txt b/Chapter_18/psql_commands.txt index fd00e7f..8d6762f 100644 --- a/Chapter_18/psql_commands.txt +++ b/Chapter_18/psql_commands.txt @@ -16,6 +16,9 @@ psql -d analysis -U postgres psql -d [database name] -U [username] -h [host name] psql -d analysis -U postgres -h example.com +-- Format for password file (pgpass.conf on Windows; .pgpass on macOS/Linux) +hostname:port:database:username:password + -- Listing 18-1: Entering a single-line query in psql -- Enter this at the psql prompt: @@ -53,7 +56,7 @@ DELETE FROM state_regions; \copy state_regions FROM 'C:\YourDirectory\state_regions.csv' WITH (FORMAT CSV, HEADER); --- Listing 18-8: +-- Listing 18-8: DELETE FROM state_regions; From 73ff1d89001b8fc0fadf5e2808aeb92880c17394 Mon Sep 17 00:00:00 2001 From: anthonydb Date: Mon, 26 Apr 2021 07:08:35 -0400 Subject: [PATCH 3/3] Chapter 19 minor updates --- Chapter_19/Chapter_19.sql | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/Chapter_19/Chapter_19.sql b/Chapter_19/Chapter_19.sql index 2924f7f..8ab6844 100644 --- a/Chapter_19/Chapter_19.sql +++ b/Chapter_19/Chapter_19.sql @@ -1,15 +1,9 @@ --- FIRST EDITION FILE; IGNORE - - - - - --------------------------------------------------------------- --- Practical SQL: A Beginner's Guide to Storytelling with Data +--------------------------------------------------------------------------- +-- Practical SQL: A Beginner's Guide to Storytelling with Data, 2nd Edition -- by Anthony DeBarros -- Chapter 19 Code Examples --------------------------------------------------------------- +---------------------------------------------------------------------------- -- VACUUM @@ -88,6 +82,8 @@ SELECT pg_size_pretty( SHOW config_file; +-- Listing 19-9: Sample postgresql.conf settings (see book for listing) + -- Listing 19-10: Show the location of the data directory SHOW data_directory; @@ -100,10 +96,10 @@ SHOW data_directory; -- BACKUP AND RESTORE -- Listing 19-11: Backing up the analysis database with pg_dump -pg_dump -d analysis -U [user_name] -Fc > analysis_backup.sql +pg_dump -d analysis -U [user_name] -Fc -v > analysis_backup.sql -- Back up just a table -pg_dump -t 'train_rides' -d analysis -U [user_name] -Fc > train_backup.sql +pg_dump -t 'train_rides' -d analysis -U [user_name] -Fc -v > train_backup.sql -- Listing 19-12: Restoring the analysis database with pg_restore