Practical Sql A Beginners Guide To Storytelling
With Data 2nd Edition 2 Converted Anthony
Debarros download
https://ebookbell.com/product/practical-sql-a-beginners-guide-to-
storytelling-with-data-2nd-edition-2-converted-anthony-
debarros-49492608
Explore and download more ebooks at ebookbell.com
Here are some recommended products that we believe you will be
interested in. You can click the link to download.
Practical Sql A Beginners Guide To Storytelling With Data 1st Edition
Anthony Debarros
https://ebookbell.com/product/practical-sql-a-beginners-guide-to-
storytelling-with-data-1st-edition-anthony-debarros-22034964
Spatial Sql A Practical Approach To Modern Gis Using Sql Matthew
Forrest
https://ebookbell.com/product/spatial-sql-a-practical-approach-to-
modern-gis-using-sql-matthew-forrest-55679710
Mastering Sql Joins A Quick Handbook On Mastering Sql Joins With
Practical Exercises Wade
https://ebookbell.com/product/mastering-sql-joins-a-quick-handbook-on-
mastering-sql-joins-with-practical-exercises-wade-52368734
Pro Sql Server 2022 Wait Statistics A Practical Guide To Analyzing
Performance In Sql Server And Azure Sql Database 3rd Edition 3rd
Edition Thomas Larock
https://ebookbell.com/product/pro-sql-server-2022-wait-statistics-a-
practical-guide-to-analyzing-performance-in-sql-server-and-azure-sql-
database-3rd-edition-3rd-edition-thomas-larock-47559882
Pro Sql Server 2019 Wait Statistics A Practical Guide To Analyzing
Performance In Sql Server Second Edition 2nd Edition Enrico Van De
Laar
https://ebookbell.com/product/pro-sql-server-2019-wait-statistics-a-
practical-guide-to-analyzing-performance-in-sql-server-second-
edition-2nd-edition-enrico-van-de-laar-52952162
Pro Sql Server 2022 Wait Statistics A Practical Guide To Analyzing
Performance In Sql Server And Azure Sql Database 3rd Edition 3rd
Edition Thomas Larock
https://ebookbell.com/product/pro-sql-server-2022-wait-statistics-a-
practical-guide-to-analyzing-performance-in-sql-server-and-azure-sql-
database-3rd-edition-3rd-edition-thomas-larock-47559896
Jooq Masterclass A Practical Guide For Java Developers To Write Sql
Queries For Complex Database Interactions Anghel Leonard
https://ebookbell.com/product/jooq-masterclass-a-practical-guide-for-
java-developers-to-write-sql-queries-for-complex-database-
interactions-anghel-leonard-56223438
Jooq Masterclass A Practical Guide For Java Developers To Write Sql
Queries For Complex Database Interactions 1st Edition Anghel Leonard
https://ebookbell.com/product/jooq-masterclass-a-practical-guide-for-
java-developers-to-write-sql-queries-for-complex-database-
interactions-1st-edition-anghel-leonard-44785934
Sql Query Design Patterns And Best Practices A Practical Guide To
Writing Readable And Maintainable Sql Queries Using Its Design
Patterns 1st Edition Steve Hughes
https://ebookbell.com/product/sql-query-design-patterns-and-best-
practices-a-practical-guide-to-writing-readable-and-maintainable-sql-
queries-using-its-design-patterns-1st-edition-steve-hughes-49190592
CONTENTS IN DETAIL
TITLE PAGE
COPYRIGHT
ABOUT THE AUTHOR
PREFACE TO THE SECOND EDITION
ACKNOWLEDGMENTS
INTRODUCTION
What Is SQL?
Why SQL?
Who Is This Book For?
What You’ll Learn
CHAPTER 1: SETTING UP YOUR CODING ENVIRONMENT
Installing a Text Editor
Downloading Code and Data from GitHub
Installing PostgreSQL and pgAdmin
Windows Installation
macOS Installation
Linux Installation
Working with pgAdmin
Launching pgAdmin and Setting a Master Password
Connecting to the Default postgres Database
Exploring the Query Tool
Customizing pgAdmin
Alternatives to pgAdmin
Wrapping Up
CHAPTER 2: CREATING YOUR FIRST DATABASE AND TABLE
Understanding Tables
Creating a Database
Executing SQL in pgAdmin
Connecting to the analysis Database
Creating a Table
Using the CREATE TABLE Statement
Making the teachers Table
Inserting Rows into a Table
Using the INSERT Statement
Viewing the Data
Getting Help When Code Goes Bad
Formatting SQL for Readability
Wrapping Up
CHAPTER 3: BEGINNING DATA EXPLORATION WITH SELECT
Basic SELECT Syntax
Querying a Subset of Columns
Sorting Data with ORDER BY
Using DISTINCT to Find Unique Values
Filtering Rows with WHERE
Using LIKE and ILIKE with WHERE
Combining Operators with AND and OR
Putting It All Together
Wrapping Up
CHAPTER 4: UNDERSTANDING DATA TYPES
Understanding Characters
Understanding Numbers
Using Integers
Auto-Incrementing Integers
Using Decimal Numbers
Choosing Your Number Data Type
Understanding Dates and Times
Using the interval Data Type in Calculations
Understanding JSON and JSONB
Using Miscellaneous Types
Transforming Values from One Type to Another with CAST
Using CAST Shortcut Notation
Wrapping Up
CHAPTER 5: IMPORTING AND EXPORTING DATA
Working with Delimited Text Files
Handling Header Rows
Quoting Columns That Contain Delimiters
Using COPY to Import Data
Importing Census Data Describing Counties
Creating the us_counties_pop_est_2019 Table
Understanding Census Columns and Data Types
Performing the Census Import with COPY
Inspecting the Import
Importing a Subset of Columns with COPY
Importing a Subset of Rows with COPY
Adding a Value to a Column During Import
Using COPY to Export Data
Exporting All Data
Exporting Particular Columns
Exporting Query Results
Importing and Exporting Through pgAdmin
Wrapping Up
CHAPTER 6: BASIC MATH AND STATS WITH SQL
Understanding Math Operators and Functions
Understanding Math and Data Types
Adding, Subtracting, and Multiplying
Performing Division and Modulo
Using Exponents, Roots, and Factorials
Minding the Order of Operations
Doing Math Across Census Table Columns
Adding and Subtracting Columns
Finding Percentages of the Whole
Tracking Percent Change
Using Aggregate Functions for Averages and Sums
Finding the Median
Finding the Median with Percentile Functions
Finding Median and Percentiles with Census Data
Finding Other Quantiles with Percentile Functions
Finding the Mode
Wrapping Up
CHAPTER 7: JOINING TABLES IN A RELATIONAL DATABASE
Linking Tables Using JOIN
Relating Tables with Key Columns
Querying Multiple Tables Using JOIN
Understanding JOIN Types
JOIN
LEFT JOIN and RIGHT JOIN
FULL OUTER JOIN
CROSS JOIN
Using NULL to Find Rows with Missing Values
Understanding the Three Types of Table Relationships
One-to-One Relationship
One-to-Many Relationship
Many-to-Many Relationship
Selecting Specific Columns in a Join
Simplifying JOIN Syntax with Table Aliases
Joining Multiple Tables
Combining Query Results with Set Operators
UNION and UNION ALL
INTERSECT and EXCEPT
Performing Math on Joined Table Columns
Wrapping Up
CHAPTER 8: TABLE DESIGN THAT WORKS FOR YOU
Following Naming Conventions
Quoting Identifiers Enables Mixed Case
Pitfalls with Quoting Identifiers
Guidelines for Naming Identifiers
Controlling Column Values with Constraints
Primary Keys: Natural vs. Surrogate
Foreign Keys
How to Automatically Delete Related Records with CASCADE
The CHECK Constraint
The UNIQUE Constraint
The NOT NULL Constraint
How to Remove Constraints or Add Them Later
Speeding Up Queries with Indexes
B-Tree: PostgreSQL’s Default Index
Considerations When Using Indexes
Wrapping Up
CHAPTER 9: EXTRACTING INFORMATION BY GROUPING AND
SUMMARIZING
Creating the Library Survey Tables
Creating the 2018 Library Data Table
Creating the 2017 and 2016 Library Data Tables
Exploring the Library Data Using Aggregate Functions
Counting Rows and Values Using count()
Finding Maximum and Minimum Values Using max() and min()
Aggregating Data Using GROUP BY
Wrapping Up
CHAPTER 10: INSPECTING AND MODIFYING DATA
Importing Data on Meat, Poultry, and Egg Producers
Interviewing the Dataset
Checking for Missing Values
Checking for Inconsistent Data Values
Checking for Malformed Values Using length()
Modifying Tables, Columns, and Data
Modifying Tables with ALTER TABLE
Modifying Values with UPDATE
Viewing Modified Data with RETURNING
Creating Backup Tables
Restoring Missing Column Values
Updating Values for Consistency
Repairing ZIP Codes Using Concatenation
Updating Values Across Tables
Deleting Unneeded Data
Deleting Rows from a Table
Deleting a Column from a Table
Deleting a Table from a Database
Using Transactions to Save or Revert Changes
Improving Performance When Updating Large Tables
Wrapping Up
CHAPTER 11: STATISTICAL FUNCTIONS IN SQL
Creating a Census Stats Table
Measuring Correlation with corr(Y, X)
Checking Additional Correlations
Predicting Values with Regression Analysis
Finding the Effect of an Independent Variable with r-Squared
Finding Variance and Standard Deviation
Creating Rankings with SQL
Ranking with rank() and dense_rank()
Ranking Within Subgroups with PARTITION BY
Calculating Rates for Meaningful Comparisons
Finding Rates of Tourism-Related Businesses
Smoothing Uneven Data
Wrapping Up
CHAPTER 12: WORKING WITH DATES AND TIMES
Understanding Data Types and Functions for Dates and Times
Manipulating Dates and Times
Extracting the Components of a timestamp Value
Creating Datetime Values from timestamp Components
Retrieving the Current Date and Time
Working with Time Zones
Finding Your Time Zone Setting
Setting the Time Zone
Performing Calculations with Dates and Times
Finding Patterns in New York City Taxi Data
Finding Patterns in Amtrak Data
Wrapping Up
CHAPTER 13: ADVANCED QUERY TECHNIQUES
Using Subqueries
Filtering with Subqueries in a WHERE Clause
Creating Derived Tables with Subqueries
Joining Derived Tables
Generating Columns with Subqueries
Understanding Subquery Expressions
Using Subqueries with LATERAL
Using Common Table Expressions
Performing Cross Tabulations
Installing the crosstab() Function
Tabulating Survey Results
Tabulating City Temperature Readings
Reclassifying Values with CASE
Using CASE in a Common Table Expression
Wrapping Up
CHAPTER 14: MINING TEXT TO FIND MEANINGFUL DATA
Formatting Text Using String Functions
Case Formatting
Character Information
Removing Characters
Extracting and Replacing Characters
Matching Text Patterns with Regular Expressions
Regular Expression Notation
Using Regular Expressions with WHERE
Regular Expression Functions to Replace or Split Text
Turning Text to Data with Regular Expression Functions
Full-Text Search in PostgreSQL
Text Search Data Types
Creating a Table for Full-Text Search
Searching Speech Text
Ranking Query Matches by Relevance
Wrapping Up
CHAPTER 15: ANALYZING SPATIAL DATA WITH POSTGIS
Enabling PostGIS and Creating a Spatial Database
Understanding the Building Blocks of Spatial Data
Understanding Two-Dimensional Geometries
Well-Known Text Formats
Projections and Coordinate Systems
Spatial Reference System Identifier
Understanding PostGIS Data Types
Creating Spatial Objects with PostGIS Functions
Creating a Geometry Type from Well-Known Text
Creating a Geography Type from Well-Known Text
Using Point Functions
Using LineString Functions
Using Polygon Functions
Analyzing Farmers’ Markets Data
Creating and Filling a Geography Column
Adding a Spatial Index
Finding Geographies Within a Given Distance
Finding the Distance Between Geographies
Finding the Nearest Geographies
Working with Census Shapefiles
Understanding the Contents of a Shapefile
Loading Shapefiles
Exploring the Census 2019 Counties Shapefile
Examining Demographics Within a Distance
Performing Spatial Joins
Exploring Roads and Waterways Data
Joining the Census Roads and Water Tables
Finding the Location Where Objects Intersect
Wrapping Up
CHAPTER 16: WORKING WITH JSON DATA
Understanding JSON Structure
Considering When to Use JSON with SQL
Using json and jsonb Data Types
Importing and Indexing JSON Data
Using json and jsonb Extraction Operators
Key Value Extraction
Array Element Extraction
Path Extraction
Containment and Existence
Analyzing Earthquake Data
Exploring and Loading the Earthquake Data
Working with Earthquake Times
Finding the Largest and Most-Reported Earthquakes
Converting Earthquake JSON to Spatial Data
Generating and Manipulating JSON
Turning Query Results into JSON
Adding, Updating, and Deleting Keys and Values
Using JSON Processing Functions
Finding the Length of an Array
Returning Array Elements as Rows
Wrapping Up
CHAPTER 17: SAVING TIME WITH VIEWS, FUNCTIONS, AND
TRIGGERS
Using Views to Simplify Queries
Creating and Querying Views
Creating and Refreshing a Materialized View
Inserting, Updating, and Deleting Data Using a View
Creating Your Own Functions and Procedures
Creating the percent_change() Function
Using the percent_change() Function
Updating Data with a Procedure
Using the Python Language in a Function
Automating Database Actions with Triggers
Logging Grade Updates to a Table
Automatically Classifying Temperatures
Wrapping Up
CHAPTER 18: USING POSTGRESQL FROM THE COMMAND
LINE
Setting Up the Command Line for psql
Windows psql Setup
macOS psql Setup
Linux psql Setup
Working with psql
Launching psql and Connecting to a Database
Running SQL Queries on psql
Navigating and Formatting Results
Meta-Commands for Database Information
Importing, Exporting, and Using Files
Additional Command Line Utilities to Expedite Tasks
Adding a Database with createdb
Loading Shapefiles with shp2pgsql
Wrapping Up
CHAPTER 19: MAINTAINING YOUR DATABASE
Recovering Unused Space with VACUUM
Tracking Table Size
Monitoring the Autovacuum Process
Running VACUUM Manually
Reducing Table Size with VACUUM FULL
Changing Server Settings
Locating and Editing postgresql.conf
Reloading Settings with pg_ctl
Backing Up and Restoring Your Database
Using pg_dump to Export a Database or Table
Restoring a Database Export with pg_restore
Exploring Additional Backup and Restore Options
Wrapping Up
CHAPTER 20: TELLING YOUR DATA’S STORY
Start with a Question
Document Your Process
Gather Your Data
No Data? Build Your Own Database
Assess the Data’s Origins
Interview the Data with Queries
Consult the Data’s Owner
Identify Key Indicators and Trends over Time
Ask Why
Communicate Your Findings
Wrapping Up
APPENDIX: ADDITIONAL POSTGRESQL RESOURCES
PostgreSQL Development Environments
PostgreSQL Utilities, Tools, and Extensions
PostgreSQL News and Community
Documentation
INDEX
PRACTICAL SQL
2nd Edition
A Beginner’s Guide to Storytelling with
Data
by Anthony DeBarros
PRACTICAL SQL, 2ND EDITION. Copyright © 2022 by Anthony DeBarros.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information storage or
retrieval system, without the prior written permission of the copyright owner and the publisher.
Printed in the United States of America
First printing
25 24 23 22 21 1 2 3 4 5 6 7 8 9
ISBN-13: 978-1-7185-0106-5 (print)
ISBN-13: 978-1-7185-0107-2 (ebook)
Publisher: William Pollock
Managing Editor: Jill Franklin
Production Manager: Rachel Monaghan
Production Editors: Jennifer Kepler and Paula Williamson
Developmental Editor: Liz Chadwick
Cover Illustrator: Josh Ellingson
Interior Design: Octopod Studios
Technical Reviewer: Stephen Frost
Copyeditor: Kim Wimpsett
Compositor: Maureen Forys, Happenstance Type-O-Rama
Proofreader: Liz Wheeler
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900; info@nostarch.com
www.nostarch.com
The Library of Congress has catalogued the first edition as follows:
Names: DeBarros, Anthony, author.
Title: Practical SQL : a beginner's guide to storytelling with data / Anthony
DeBarros.
Description: San Francisco : No Starch Press, 2018. | Includes index.
Identifiers: LCCN 2018000030 (print) | LCCN 2017043947 (ebook) | ISBN 9781593278458
(epub) | ISBN
1593278454 (epub) | ISBN 9781593278274 (paperback) | ISBN 1593278276 (paperback)
| ISBN 9781593278458
(ebook)
Subjects: LCSH: SQL (Computer program language) | Database design. | BISAC:
COMPUTERS / Programming
Languages / SQL. | COMPUTERS / Database Management / General. | COMPUTERS /
Database Management
/ Data Mining.
Classification: LCC QA76.73.S67 (print) | LCC QA76.73.S67 D44 2018 (ebook) | DDC
005.75/6--dc23
LC record available at https://lccn.loc.gov/2018000030
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc.
Other product and company names mentioned herein may be the trademarks of their respective
owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are
using the names only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every
precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc.
shall have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in it.
About the Author
Anthony DeBarros is a longtime journalist and early adopter of “data
journalism,” the use of spreadsheets, databases, and code to find news in
data. He’s currently a data editor for the Wall Street Journal, where he
covers topics including the economy, trade, demographics, and the Covid-
19 pandemic. Previously, he worked for the Gannett company at USA Today
and the Poughkeepsie Journal and held product development and content
strategy roles for Questex and DocumentCloud.
About the Technical Reviewer
Stephen Frost is the chief technology officer at Crunchy Data. He has been
working with PostgreSQL since 2003 and general database technology
since before then. Stephen began contributing to PostgreSQL development
in 2004 and has been involved in the development of the role system,
column-level privileges, row-level security, GSSAPI encryption, and the
predefined roles system. He has also served on the board of the United
States PostgreSQL Association and Software in the Public Interest,
regularly speaks at PostgreSQL Community conferences and events, and
works as a member of various PostgreSQL community teams.
PREFACE TO THE SECOND EDITION
Since the publication of the first edition of Practical
SQL, I’ve received kind notes about the book from
readers around the world. One happy reader said it
helped him ace SQL questions on a job interview.
Another, a teacher, wrote to say that his students
remarked favorably about having the book assigned
for class. Others just wanted to say thanks because
they found the book helpful and a good read, two
pieces of feedback that will warm the heart of most
any author.
I also sometimes heard from readers who hit a roadblock while working
through an exercise or who had trouble with software or data files. I paid
close attention to those emails, especially when the same question seemed
to crop up more than once. Meanwhile, during my own journey of learning
SQL—I use it every day at work—I’d often discover a technique and wish
that I’d included it in the book.
With all that in mind, I approached the team at No Starch Press with the
idea of updating and expanding Practical SQL into a second edition. I’m
thankful they said yes. This new version of the book is more complete,
offers stronger guidance for readers related to software and code, and
clarifies information that wasn’t as clear or presented as accurately as it
could have been. The book has been thoroughly enjoyable to revisit, and
I’ve learned much along the way.
This second edition includes numerous updates, expansions, and
clarifications in every chapter. Throughout, I’ve been careful to note when
code syntax adheres to the SQL standard—meaning you can generally use it
across database systems—or when the syntax is specific to the database
used in the book, PostgreSQL.
The following are among the most substantial changes:
Two chapters are new. Chapter 1, “Setting Up Your Coding Environment,”
details how to install PostgreSQL, pgAdmin, and additional PostgreSQL
components on multiple operating systems. It also shows how to obtain the
code listings and data from GitHub. In the first edition, this information was
located in the introduction and occasionally missed by readers. Chapter 16,
“Working with JSON Data,” covers PostgreSQL’s support for the
JavaScript Object Notation data format, using datasets about movies and
earthquakes.
In Chapter 4 on data types, I’ve added a section on IDENTITY, the ANSI
SQL standard implementation for auto-incrementing integer columns.
Throughout the book, IDENTITY replaces the PostgreSQL-specific serial
auto-incrementing integer type so that code examples more closely reflect
the SQL standard.
Chapter 5 on importing and exporting data now includes a section about
using the WHERE keyword with the COPY command to filter which rows are
imported from a source file to a table.
I’ve removed the user-created median() function from Chapter 6 on basic
math in favor of focusing exclusively on the SQL standard
percentile_cont() function for calculating medians.
In Chapter 7 on table joins, I’ve added a section covering the set operators
UNION, UNION ALL, INTERSECT, and EXCEPT. Additionally, I’ve added a
section covering the USING clause in joins to reduce redundant output and
simplify query syntax.
Chapter 10 on inspecting and modifying data includes a new section on
using the RETURNING keyword in an UPDATE statement to display the data
that the statement modified. I’ve also added a section that describes how to
use the TRUNCATE command to remove all rows from a table and restart an
IDENTITY sequence.
In Chapter 11 on statistical functions, a new section demonstrates how to
create a rolling average to smooth uneven data to get a better sense of
trends over time. I’ve also added information on functions for calculating
standard deviation and variance.
Chapter 13 on advanced query techniques now shows how to use the
LATERAL keyword with subqueries. One benefit is that, by combining
LATERAL with JOIN, you get functionality similar to a for loop in a
programming language.
In Chapter 15 on analyzing spatial data, I demonstrate how to use the
Geometry Viewer in pgAdmin to see geographies placed on a map. This
feature was added to pgAdmin after publication of the first edition.
In Chapter 17 on views, functions, and triggers, I’ve added information
about materialized views and showed how their behavior differs from
standard views. I also cover procedures, which PostgreSQL now supports in
addition to functions.
Finally, where practical, datasets have been updated to the most recent
available at the time of writing. This primarily applies to US Census
population statistics but also includes the text of presidential speeches and
library usage statistics.
Thank you for reading Practical SQL! If you have any questions or
feedback, please get in touch by emailing practicalsqlbook@gmail.com.
ACKNOWLEDGMENTS
This second edition of Practical SQL is the work of many hands. My
thanks, first, go to the team at No Starch Press. Thanks to Bill Pollock for
capturing the vision and sharpening the initial concept for the book—and
for agreeing to let me have another go at it. Special thanks and appreciation
to senior editor Liz Chadwick, who improved each chapter with her
insightful suggestions and deft editing, and to copyeditor Kim Wimpsett
and the production team of Paula Williamson and Jennifer Kepler.
Stephen Frost, chief technology officer at Crunchy Data and longtime
contributor to the PostgreSQL community, served as the technical reviewer
for this edition. I deeply appreciate the time Stephen took to explain the
inner workings of PostgreSQL and SQL concepts. This book is better, more
thorough, and more accurate thanks to his detailed eye. I’d also like to
acknowledge Josh Berkus, whose many contributions as technical reviewer
for the first edition persist in this new version.
Thank you to Investigative Reporters and Editors (IRE) and its members
and staff past and present for training journalists to find stories in data. IRE
is where I got my start with SQL and data journalism.
Many of my colleagues have not only imparted memorable lessons on
data analysis, they’ve also made my workdays brighter. Special thanks to
Paul Overberg for sharing his vast knowledge of demographics and the US
Census, to Lou Schilling for many technical lessons, to Christopher
Schnaars for his SQL expertise, to Peter Matseykanets for his
encouragement, and to Chad Day, John West, and Maureen Linke and the
WSJ DC visuals team for continual inspiration.
My deepest appreciation goes to my dear wife, Elizabeth, and our sons.
You are the brightest lights in my day. As we are fond of saying, “To the
journey . . .”
INTRODUCTION
Shortly after joining the staff of USA
Today, I received a dataset that I would
analyze almost every week for the next
decade. It was the weekly Best-Selling
Books list, which ranked the nation’s
top-selling titles based on confidential sales data. Not
only did the list produce an endless stream of story
ideas to pitch, it also captured the zeitgeist of
America in a singular way.
Did you know that cookbooks sell a bit more during the week of
Mother’s Day or that Oprah Winfrey turned many obscure writers into
number-one best-selling authors just by having them on her show? Every
week, the book list editor and I pored over the sales figures and book
genres, ranking the data in search of a new headline. Rarely did we come up
empty: we chronicled everything from the rocket-rise of the blockbuster
Harry Potter series to the fact that Oh, the Places You’ll Go! by Dr. Seuss
had become a perennial gift for new graduates.
My technical companion in that time was the database programming
language SQL (for Structured Query Language). Early on, I convinced USA
Today’s IT department to grant me access to the SQL-based database
system that powered our book list application. Using SQL, I was able to
discover the stories hidden in the database, which contained sales data
related to titles, authors, genres, and the codes that defined the publishing
world.
SQL has been useful to me ever since, whether my role was in product
development, in content strategy, or, lately, as a data editor for the Wall
Street Journal. In each case, SQL has helped me find interesting stories in
data—and that’s exactly what you’ll learn to do using this book.
What Is SQL?
SQL is a widely used programming language for managing data and
database systems. Whether you’re a marketing analyst, a journalist, or a
researcher mapping neurons in the brain of a fruit fly, you’ll benefit from
using SQL to collect, modify, explore, and summarize data.
Because SQL is a mature language that’s been around for decades, it’s
ingrained in many modern systems. A pair of IBM researchers first outlined
the syntax for SQL (then called SEQUEL) in a 1974 paper, building on the
theoretical work of the British computer scientist Edgar F. Codd. In 1979, a
precursor to the database company Oracle (then called Relational Software)
became the first to use the language in a commercial product. Today, SQL
still ranks as one of the most-used computer languages in the world, and
that’s unlikely to change soon.
Each database system, such as PostgreSQL, MySQL or Microsoft SQL
Server, implements its own variant of SQL, so you’ll notice subtle—or
sometimes significant—differences in syntax if you jump from one system
to another. There are several reasons behind this. The American National
Standards Institute (ANSI) adopted a standard for SQL in 1986, followed
by the International Organization for Standardization (ISO) in 1987. But the
standard doesn’t cover all aspects of SQL that are required for a database
implementation—for example, it has no entry for creating indexes. That
leaves each database system maker to choose how to implement features the
standard doesn’t cover—and no database maker currently claims to
conform to the entire standard.
Meanwhile, business considerations can lead commercial database
vendors to create nonstandard SQL features for both competitive advantage
and as a way to keep users in their ecosystem. For example, Microsoft’s
SQL Server uses the proprietary Transact-SQL (T-SQL) that includes a
number of features not in the SQL standard, such as its syntax for declaring
local variables. Migrating code written using T-SQL to another database
system may not be trivial, therefore.
In this book, the examples and code use the PostgreSQL database system.
PostgreSQL, or simply Postgres, is a robust application that can handle
large amounts of data. Here are some reasons PostgreSQL is a great choice
to use with this book:
It’s free.
It’s available for Windows, macOS, and Linux operating systems.
Its SQL implementation aims to closely follow the SQL standard.
It’s widely used, so finding help online is easy.
Its geospatial extension, PostGIS, lets you analyze geometric data and
perform mapping functions and is often used with mapping software such
as QGIS.
It’s available in cloud computing environments such as Amazon Web
Services and Google Cloud.
It’s a common choice as a data store for web applications, including those
powered by the popular web framework Django.
The good news is that the fundamental concepts and much of the core
SQL syntactical conventions of PostgreSQL will work across databases. So,
if you’re using MySQL at work, you can employ much of what you learn
here—or easily find parallel code concepts. When syntax is PostgreSQL-
specific, I make sure to point that out. If you need to learn the SQL syntax
of a system with features that deviate from the standard, such as Microsoft
SQL Server’s T-SQL, you may want to further explore a resource focusing
on that system.
Why SQL?
SQL certainly isn’t the only option for crunching data. Many people start
with Microsoft Excel spreadsheets and their assortment of analytic
functions. After working with Excel, they might graduate to Access, the
database system built into some versions of Microsoft Office, which has a
graphical query interface that makes it easy to get work done. So why learn
SQL?
One reason is that Excel and Access have their limits. Excel currently
allows 1,048,576 rows maximum per worksheet. Access limits database
size to two gigabytes and limits columns to 255 per table. It’s not
uncommon for datasets to surpass those limits. The last obstacle you want
to discover while facing a deadline is that your database system doesn’t
have the capacity to get the job done.
Using a robust SQL database system allows you to work with terabytes
of data, multiple related tables, and thousands of columns. It gives you fine-
grained control over the structure of your data, leading to efficiency, speed,
and—most important—accuracy.
SQL is also an excellent adjunct to programming languages used in the
data sciences, such as R and Python. If you use either language, you can
connect to SQL databases and, in some cases, even incorporate SQL syntax
directly into the language. For people with no background in programming
languages, SQL often serves as an easy-to-understand introduction into
concepts related to data structures and programming logic.
Finally, SQL is useful beyond data analysis. If you delve into building
online applications, you’ll find that databases provide the backend power
for many common web frameworks, interactive maps, and content
management systems. When you need to dig beneath the surface of these
applications, the ability to manage data and databases with SQL will come
in very handy.
Who Is This Book For?
Practical SQL is for people who encounter data in their everyday lives and
want to learn how to analyze, manage, and transform it. With that in mind,
we cover real-world data and scenarios, such as US Census demographics,
crime reports, and data about taxi rides in New York City. We aim to
understand not only how SQL works but how we can use it to find valuable
insights.
This book was written with people new to programming in mind, so the
early chapters cover key basics about databases, data, and SQL syntax.
Readers with some SQL experience should benefit from later chapters that
cover more advanced topics, such as Geographical Information Systems
(GIS). I assume that you know your way around your computer, including
how to install programs, navigate your hard drive, and download files from
the internet, but I don’t assume you have any experience with programming
or data analysis.
What You’ll Learn
Practical SQL starts with a chapter on setting up your system and getting
the code and data examples and then moves through the basics of databases,
queries, tables, and data that are common to SQL across many database
systems. Chapters 14 to 19 cover topics more specific to PostgreSQL, such
as full-text search, functions, and GIS. Although many chapters in this book
can stand alone, you should work through the book sequentially to build on
the fundamentals. Datasets presented in early chapters often reappear later,
so following the book in order will help you stay on track.
The following summary provides more detail about each chapter:
Chapter 1: Setting Up Your Coding Environment walks through
setting up PostgreSQL, the pgAdmin user interface, and a text editor, plus
how to download example code and data.
Chapter 2: Creating Your First Database and Table provides step-by-
step instructions for the process of loading a simple dataset about teachers
into a new database.
Chapter 3: Beginning Data Exploration with SELECT explores basic
SQL query syntax, including how to sort and filter data.
Chapter 4: Understanding Data Types explains the definitions for
setting columns in a table to hold specific types of data, from text to dates
to various forms of numbers.
Chapter 5: Importing and Exporting Data explains how to use SQL
commands to load data from external files and then export it. You’ll load a
table of US Census population data that you’ll use throughout the book.
Chapter 6: Basic Math and Stats with SQL covers arithmetic
operations and introduces aggregate functions for finding sums, averages,
and medians.
Chapter 7: Joining Tables in a Relational Database explains how to
query multiple, related tables by joining them on key columns. You’ll learn
how and when to use different types of joins.
Chapter 8: Table Design that Works for You covers how to set up
tables to improve the organization and integrity of your data as well as how
to speed up queries using indexes.
Chapter 9: Extracting Information by Grouping and Summarizing
explains how to use aggregate functions to find trends in US library usage
based on annual surveys.
Chapter 10: Inspecting and Modifying Data explores how to find and
fix incomplete or inaccurate data using a collection of records about meat,
egg, and poultry producers as an example.
Chapter 11: Statistical Functions in SQL introduces correlation,
regression, ranking, and other functions to help you derive more meaning
from datasets.
Chapter 12: Working with Dates and Times explains how to create,
manipulate, and query dates and times in your database, including working
with time zones and with data about New York City taxi trips and Amtrak
train schedules.
Chapter 13: Advanced Query Techniques explains how to use more
complex SQL operations such as subqueries and cross tabulations, plus the
CASE statement, to reclassify values in a dataset on temperature readings.
Chapter 14: Mining Text to Find Meaningful Data covers how to use
PostgreSQL’s full-text search engine and regular expressions to extract data
from unstructured text, using police reports and a collection of speeches by
US presidents as examples.
Chapter 15: Analyzing Spatial Data with PostGIS introduces data
types and queries related to spatial objects, which will let you analyze
geographical features such as counties, roads, and rivers.
Chapter 16: Working with JSON Data introduces the JavaScript
Object Notation (JSON) data format and uses data about movies and
earthquakes to explore PostgreSQL JSON support.
Chapter 17: Saving Time with Views, Functions, and Triggers
explains how to automate database tasks so you can avoid repeating routine
work.
Chapter 18: Using PostgreSQL from the Command Line covers how
to use text commands at your computer’s command prompt to connect to
your database and run queries.
Chapter 19: Maintaining Your Database provides tips and procedures
for tracking the size of your database, customizing settings, and backing up
data.
Chapter 20: Telling Your Data’s Story provides guidelines for
generating ideas for analysis, vetting data, drawing sound conclusions, and
presenting your findings clearly.
Appendix: Additional PostgreSQL Resources lists software and
documentation to help you grow your skills.
Each chapter ends with a “Try It Yourself” section that contains exercises
to help you reinforce the topics you learned.
Ready? Let’s begin with Chapter 1, “Setting Up Your Coding
Environment.”
1
SETTING UP YOUR CODING
ENVIRONMENT
Let’s begin by installing the resources
you’ll need to complete the exercises in
the book. In this chapter, you’ll install a
text editor, download the example code
and data, and then install the
PostgreSQL database system and its companion
graphical user interface, pgAdmin. I’ll also tell you
how to get help if you need it. When you’re finished,
your computer will have a robust environment for
you to learn how to analyze data with SQL.
Avoid the temptation to skip ahead to the next chapter. My high school
teacher (clearly a fan of alliteration) used to tell us that “proper planning
prevents poor performance.” If you follow all the steps in this chapter,
you’ll avoid headaches later.
Our first task is to set up a text editor suitable for working with data.
Installing a Text Editor
The source data you’ll add to a SQL database is typically stored in multiple
text files, often in a format called comma-separated values (CSV). You’ll
learn more about the CSV format in Chapter 5, in the section “Working
with Delimited Text Files,” but for now let’s make sure you have a text
editor that will let you open those files without inadvertently harming the
data.
Common business applications—word processors and spreadsheet
programs—tend to introduce styles or hidden characters into files without
asking, and that makes using them for data work problematic, as data
software expects data in precise formats. For example, if you open a CSV
file with Microsoft Excel, the program will automatically alter some data to
make it more human-readable; it will assume, for example, that an item
code of 3-09 is a date and format it as 9-Mar. Text editors deal exclusively
with plain text with no embellishments such as formatting, and for that
reason programmers use them to edit files that hold source code, data, and
software configurations—all cases where you want your text to be treated
as text, and nothing more.
Any text editor should work for the book’s purposes, so if you have a
favorite, feel free to use it. Here are some I have used and recommend.
Except where noted, they are free and available for macOS, Windows, and
Linux.
Visual Studio Code by Microsoft: https://code.visualstudio.com/
Atom by GitHub: https://atom.io/
Sublime Text by Sublime HQ (free to evaluate but requires purchase for
continued use): https://www.sublimetext.com/
Notepad++ by author Don Ho (Windows only): https://notepad-plus-
plus.org/ (note that this is a different application than Notepad.exe, which
comes with Windows)
More advanced users who prefer to work in the command line may want
to use one of these two text editors, which are installed by default in macOS
and Linux:
vim by author Bram Moolenaar and the open source community:
https://www.vim.org/
GNU nano by author Chris Allegretta and the open source community:
https://www.nano-editor.org/
If you don’t have a text editor, download and install one and get familiar
with the basics of opening folders and working with files.
Next, let’s get the book’s example code and data.
Downloading Code and Data from GitHub
All of the code and data you’ll need for working through the book’s
exercises are available for download. To get it, follow these steps:
. Visit the book’s page on the No Starch Press website at
https://nostarch.com/practical-sql-2nd-edition/.
. On the page, click Download the code from GitHub to visit the repository
on https://github.com/ that holds the material.
. On the Practical SQL 2nd Edition page at GitHub, you should see a Code
button. Click it, and then select Download ZIP to save the ZIP file to your
computer. Place it in a location where you can easily find it, such as your
desktop. (If you’re a GitHub user, you can also clone or fork the repository.)
. Unzip the file. You should then see a folder named practical-sql-2-master
that contains the various files and subfolders for the book. Again, place this
folder where you can easily find it.
NOTE
Windows users will need to provide permission for the PostgreSQL
database you will install to read and write to the contents of the
practical-sql-2-master folder. To do so, right-click the folder, click
Properties, and click the Security tab. Click Edit and then Add.
Type the name Everyone into the object names box and click OK.
Highlight Everyone in the user list, select all boxes under Allow,
and then click Apply and OK.
Inside the practical-sql-2-master folder, for each chapter you’ll find a
subfolder named Chapter_XX (XX is the chapter number). Inside
subfolders, each chapter that includes code examples will also have a file
named Chapter_XX that ends with a .sql extension. This is a SQL code file
that you can open with your text editor or with the PostgreSQL
administrative tool you’ll install later in this chapter. Note that in the book
several code examples are truncated to save space, but you’ll need the full
listing from the .sql file to complete the exercise. You’ll know an example
is truncated when you see --snip-- in the listing.
The chapter folders also contain the public data you’ll use in the
exercises, stored in CSV and other text-based files. As noted, it’s fine to
view CSV files with a true text editor, but don’t open these files with Excel
or a word processor.
Now, with the prerequisites complete, let’s load the database software.
Installing PostgreSQL and pgAdmin
In this section, you’ll install both the PostgreSQL database system and a
companion graphical administrative tool, pgAdmin. Think of pgAdmin as a
helpful visual workspace for managing your PostgreSQL database. Its
interface lets you see your database objects, manage settings, import and
export data, and write queries, which is the code that retrieves data from
your database.
One benefit of using PostgreSQL is that the open source community has
provided excellent guidelines that make it easy to get PostgreSQL up and
running. The following sections outline installation for Windows, macOS,
and Linux as of this writing, but the steps might change as new versions of
the software or operating systems are released. Check the documentation
noted in each section as well as the GitHub repository with the book’s
resources; I’ll maintain files there with updates and answers to frequently
asked questions.
NOTE
I recommend you install the latest available version of PostgreSQL
for your operating system to ensure that it’s up-to-date on security
patches and new features. For this book, I’ll assume you’re using
version 11.0 or later.
Windows Installation
For Windows, I recommend using the installer provided by the company
EDB (formerly EnterpriseDB), which offers support and services for
PostgreSQL users. When you download the PostgreSQL package bundle
from EDB, you also get pgAdmin and Stack Builder, which includes a few
other tools you’ll use in this book and throughout your SQL career.
To get the software, visit https://www.postgresql.org/download/windows/
and click Download the installer in the EDB section. This should lead to a
downloads page on the EDB site. Select the latest available 64-bit Windows
version of PostgreSQL unless you’re using an older PC with 32-bit
Windows.
NOTE
The following section covers installation for Windows 10. If you’re
using Windows 11, please check the GitHub repository with the
book’s resources for notes about any adjustments to these
instructions.
After you download the installer, follow these steps to install
PostgreSQL, pgAdmin, and additional components:
. Right-click the installer and select Run as administrator. Answer Yes to
the question about allowing the program to make changes to your computer.
The program will perform a setup task and then present an initial welcome
screen. Click through it.
. Choose your installation directory, accepting the default.
. On the Select Components screen, select the boxes to install PostgreSQL
Server, the pgAdmin tool, Stack Builder, and the command line tools.
. Choose the location to store data. You can choose the default, which is in a
data subdirectory in the PostgreSQL directory.
. Choose a password. PostgreSQL is robust with security and permissions.
This password is for the default initial database superuser account, which is
called postgres.
. Select the default port number where the server will listen. Unless you have
another database or application using it, use the default, which should be
5432. You can substitute 5433 or another number if you already have an
application using the default port.
. Select your locale. Using the default is fine. Then click through the
summary screen to begin the installation, which will take several minutes.
. When the installation is done, you’ll be asked whether you want to launch
EnterpriseDB’s Stack Builder to obtain additional packages. Make sure the
box is checked and click Finish.
. When Stack Builder launches, choose the PostgreSQL installation on the
drop-down menu and click Next. A list of additional applications should
download.
. Expand the Spatial Extensions menu and select the PostGIS Bundle for the
version of PostgreSQL you installed. You may see more than one listed; if
so, choose the newest version. Also, expand the Add-ons, tools and
utilities menu and select EDB Language Pack, which installs support for
programming languages including Python. Click through several times;
you’ll need to wait while the installer downloads the additional
components.
When installation files have been downloaded, click Next to install the
language and PostGIS components. For PostGIS, you’ll need to agree to the
license terms; click through until you’re asked to Choose Components.
Make sure PostGIS and Create spatial database are selected. Click Next,
accept the default install location, and click Next again.
. Enter your database password when prompted and continue through the
prompts to install PostGIS.
. Answer Yes when asked to register the PROJ_LIB and GDAL_DATA
environment variables. Also, answer Yes to the questions about setting
POSTGIS_ENABLED_DRIVERS and enabling the
POSTGIS_ENABLE_OUTDB_RASTERS environment variable. Finally, click
through the final Finish steps to complete the installation and exit the
installers. Depending on the version, you may be prompted to restart your
computer.
When finished, you should have two new folders in your Windows Start
menu: one for PostgreSQL and another for PostGIS.
If you’d like to get started right away, you can skip ahead to the section
“Working with pgAdmin.” Otherwise, follow the steps in the next section to
set environment variables for optional Python language support. We cover
using Python with PostgreSQL in Chapter 17; you can wait until then to set
up Python if you’d like to move ahead now.
Configuring Python Language Support
In Chapter 17, you’ll learn how to use the Python programming language
with PostgreSQL. In the previous section, you installed the EDB Language
Pack, which provides Python support. Follow these steps to add the location
of the Language Pack files to your Windows system’s environment
variables:
. Open the Windows Control Panel by clicking the Search icon on the
Windows taskbar, entering Control Panel, and then clicking the Control
Panel icon.
. In the Control Panel app, enter Environment in the search box. In the list
of search results displayed, click Edit the System Environment Variables.
A System Properties dialog will appear.
. In the System Properties dialog, on the Advanced tab, click Environment
Variables. The dialog that opens has two sections: User variables and
System variables. In the System variables section, if you don’t see a PATH
variable, continue to step a to create a new one. If you do see an existing
PATH variable, continue to step b to modify it.
If you don’t see PATH in the System variables section, click New to open a
New System Variable dialog, shown in Figure 1-1.
Figure 1-1: Creating a new PATH environment variable in Windows 10
In the Variable name box, enter PATH. In the Variable value box, enter
C:edblanguagepackv2Python-3.9. (Instead of typing, you can click
Browse Directory and navigate to the directory in the Browse For Folder
dialog.) When you’ve either entered the path manually or browsed to it,
click OK on the dialog to close it.
. If you do see an existing PATH variable in the System variables section,
highlight it and click Edit. In the list of variables that displays, click New
and enter C:edblanguagepackv2Python-3.9. (Instead of typing, you can
click Browse Directory and navigate to the directory in the Browse For
Folder dialog.)
Once you’ve added the Language Pack path, highlight it in the list of
variables and click Move Up until the path is at the top of the variables list.
That way, PostgreSQL will find the correct Python version if you have
additional Python installations.
The result should look like the highlighted line in Figure 1-2. Click OK to
close the dialog.
Figure 1-2: Editing existing PATH environment variables in Windows 10
. Finally, in the System variables section, click New. In the New System
Variable dialog, enter PYTHONHOME in the Variable name box. In the
Variable value box, enter C:edblanguagepackv2Python-3.9. When
you’re finished, click OK in all dialogs to close them. Note that these
Python path settings will take effect the next time you restart your system.
If you experience any hiccups during the PostgreSQL install, check the
resources for the book, where I will note changes that occur as the software
is developed and can also answer questions. If you’re unable to install
PostGIS via Stack Builder, try downloading a separate installer from the
PostGIS site at https://postgis.net/windows_downloads/ and consult the
guides at https://postgis.net/documentation/.
Now, you can move ahead to the section “Working with pgAdmin.”
macOS Installation
For macOS users, I recommend obtaining Postgres.app, an open source
macOS application that includes PostgreSQL as well as the PostGIS
extension and a few other goodies. Separately, you’ll need to install the
pgAdmin GUI and the Python language for use in functions.
Installing Postgres.app and pgAdmin
Follow these steps:
. Visit https://postgresapp.com/ and download the latest release of the app.
This will be a Disk Image file that ends in .dmg.
. Double-click the .dmg file to open it, and then drag and drop the app icon
into your Applications folder.
. In your Applications folder, double-click the app icon to launch
Postgres.app. (If you see a dialog that says the app cannot be opened
because the developer cannot be verified, click Cancel. Then right-click the
app icon and choose Open.) When Postgres.app opens, click Initialize to
create and start a PostgreSQL database server.
A small elephant icon will appear in your menu bar to indicate that you
now have a database running. To set up the included PostgreSQL command
line tools so you’re able to use them in future, open your Terminal
application and run the following single line of code at the prompt (you can
copy the code as a single line from the Postgres.app site at
https://postgresapp.com/documentation/install.html):
sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin
| sudo tee /etc/paths.d/postgresapp
You may be prompted for the password you use to log in to your Mac.
Enter that. The commands should execute without providing any output.
Next, because Postgres.app doesn’t include pgAdmin, follow these steps
to install pgAdmin:
. Visit the pgAdmin site’s page for macOS downloads at
https://www.pgadmin.org/download/pgadmin-4-macos/.
. Select the latest version and download the installer (look for a Disk Image
file that ends in .dmg).
. Double-click the .dmg file, click through the prompt to accept the terms,
and then drag pgAdmin’s elephant app icon into your Applications folder.
Installation on macOS is relatively simple, but if you encounter any
issues, review the documentation for Postgres.app at
https://postgresapp.com/documentation/ and for pgAdmin at
https://www.pgadmin.org/docs/.
Installing Python
In Chapter 17, you’ll learn how to use the Python programming language
with PostgreSQL. To use Python with Postgres.app, you must install a
specific version of the language even though macOS comes with Python
pre-installed (and you might have set up an additional Python environment).
To enable Postgres.app’s optional Python language support, follow these
steps:
. Visit the official Python site at https://www.python.org/ and click the
Downloads menu.
. In the list of releases, find and download the latest version of Python 3.9.
Choose the appropriate installer for your Mac’s processor—an Intel chip on
older Macs or Apple Silicon for newer models. The download is an Apple
software package file that ends in .pkg.
. Double-click the package file to install Python, clicking through license
agreements. Close the installer when finished.
Python requirements for Postgres.app may change over time. Check its
Python documentation at
https://postgresapp.com/documentation/plpython.html as well as the
resources for this book for updates.
You’re now ready to move ahead to the section “Working with
pgAdmin.”
Linux Installation
If you’re a Linux user, installing PostgreSQL becomes simultaneously easy
and difficult, which in my experience is very much the way it is in the
Linux universe. Most times you can accomplish an installation with a few
commands, but finding those commands requires some Internet sleuth
work. Thankfully, most popular Linux distributions—including Ubuntu,
Debian, and CentOS—bundle PostgreSQL in their standard package.
However, some distributions stay on top of updates more than others, so
there’s a chance the PostgreSQL you have downloaded may not be the
latest. The best path is to consult your distribution’s documentation for the
best way to install PostgreSQL if it’s not already included or if you want to
upgrade to a more recent version.
Alternatively, the PostgreSQL project maintains complete up-to-date
package repositories for Red Hat variants, Debian, and Ubuntu. Visit
https://yum.postgresql.org/ and https://wiki.postgresql.org/wiki/Apt for
details. The packages you’ll want to install include the client and server for
PostgreSQL, pgAdmin (if available), PostGIS, and PL/Python. The exact
names of these packages will vary according to your Linux distribution.
You might also need to manually start the PostgreSQL database server.
The pgAdmin app is rarely part of Linux distributions. To install it, refer
to the pgAdmin site at https://www.pgadmin.org/download/ for the latest
instructions and to see whether your platform is supported. If you’re feeling
adventurous, you can find instructions on building the app from source code
at https://www.pgadmin.org/download/pgadmin-4-source-code/. Once
finished, you can move ahead to the section “Working with pgAdmin.”
Ubuntu Installation Example
To give you a sense of what a PostgreSQL Linux install looks like, here are
the steps I took to load PostgreSQL, pgAdmin, PostGIS, and PL/Python on
Ubuntu 21.04, codenamed Hirsute Hippo. It’s a combination of the
directions found at https://wiki.postgresql.org/wiki/Apt plus the “Basic
Server Setup” section at https://help.ubuntu.com/community/PostgreSQL/.
You can follow along if you’re on Ubuntu.
Open your Terminal by pressing CTRL-ALT-T. Then, at the prompt,
enter the following lines to import a key for the PostgreSQL APT
repository:
sudo apt-get install curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc |
sudo apt-key add -
Next, run this single line to create the file /etc/apt/sources.list.d/pgdg.list:
sudo sh -c 'echo "deb
https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-
pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Once that’s done, update the package lists and install PostgreSQL and
pgAdmin with the next two lines. Here, I installed PostgreSQL 13; you can
choose a newer version if available.
sudo apt-get update
sudo apt-get install postgresql-13
You should now have PostgreSQL running. At the Terminal, enter the
next line, which allows you to log in to the server and connect as the default
postgres user to the postgres database using the psql interactive terminal,
which we’ll cover in depth in Chapter 18:
sudo -u postgres psql postgres
When psql launches, it displays version information as well as a
postgres=# prompt. Enter the following at the prompt to set a password:
postgres=# password postgres
I also like to create a user account with a name that matches my Ubuntu
username. To do this, at the postgres=# prompt, enter the following line,
substituting your Ubuntu username where you see anthony:
postgres=# CREATE USER anthony SUPERUSER;
Exit psql by entering q at the prompt. You should be back at your
Terminal prompt once again.
To install pgAdmin, first import a key for the repository:
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub
| sudo apt-key add
Next, run this single line to create the file
/etc/apt/sources.list.d/pgadmin4.list and update package lists:
sudo sh -c 'echo "deb
https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_rel
ease -cs) pgadmin4 main" >
/etc/apt/sources.list.d/pgadmin4.list && apt update'
Then you can install pgAdmin 4:
sudo apt-get install pgadmin4-desktop
Finally, to install the PostGIS and PL/Python extensions, run the
following lines in your terminal (substituting the version numbers of your
PostgreSQL version):
sudo apt install postgresql-13-postgis-3
sudo apt install postgresql-plpython3-13
Check the official Ubuntu and PostgreSQL documentation for updates. If
you experience any errors, typically with Linux an online search will yield
helpful tips.
Working with pgAdmin
The final piece of your setup puzzle is to get familiar with pgAdmin, an
administration and management tool for PostgreSQL. The pgAdmin
software is free, but don’t underestimate its performance; it’s a full-featured
tool as powerful as paid tools such as Microsoft’s SQL Server Management
Studio. With pgAdmin, you get a graphical interface where you can
configure multiple aspects of your PostgreSQL server and databases, and—
most appropriately for this book—use a SQL query tool for writing,
running, and saving queries.
Launching pgAdmin and Setting a Master Password
Assuming you followed the installation steps for your operating system
earlier in the chapter, here’s how to launch pgAdmin:
Windows: Go to the Start menu, find the PostgreSQL folder for the version
you installed, click it, and then select pgAdmin4.
macOS: Click the pgAdmin icon in your Applications folder, making sure
you’ve also launched Postgres.app.
Linux: Startup may differ depending on your Linux distribution. Typically,
at your Terminal prompt, enter pgadmin4 and press ENTER. In Ubuntu,
pgAdmin appears as an app in the Activities Overview.
You should see the pgAdmin splash screen, followed by the application
opening, as in Figure 1-3. If it’s your first time launching pgAdmin, you’ll
also receive a prompt to set a master password. This password is not related
to the one you set up during the PostgreSQL install. Set a master password
and click OK.
NOTE
On macOS, when you launch pgAdmin the first time, a dialog might
appear that displays “pgAdmin4.app can’t be opened because it is
from an unidentified developer.” Right-click the icon and click
Open. The next dialog should give you the option to open the app;
going forward, your Mac will remember you’ve granted this
permission.
Figure 1-3: The pgAdmin app running on Windows 10
The pgAdmin layout includes a left vertical pane that displays an object
browser where you can view available servers, databases, users, and other
objects. Across the top of the screen is a collection of menu items, and
below those are tabs to display various aspects of database objects and
performance. Next, let’s connect to your database.
Connecting to the Default postgres Database
PostgreSQL is a database management system, which means it’s software
that allows you to define, manage, and query databases. When you installed
PostgreSQL, it created a database server—an instance of the application
running on your computer—that includes a default database called
postgres. A database is a collection of objects that includes tables,
functions, and much more, and this is where your actual data will lie. We
use the SQL language (as well as pgAdmin) to manage objects and data
stored in the database.
In the next chapter, you’ll create your own database on your PostgreSQL
server to organize your work. For now, let’s connect to the default postgres
database to explore pgAdmin. Use the following steps:
. In the object browser, click the downward-pointing arrow to the left of the
Servers node to show the default server. Depending on your operating
system, the default server name could be localhost or PostgreSQL x, where
x is the Postgres version number.
. Double-click the server name. If prompted, enter the database password you
chose during installation (you can choose to save the password so you don’t
need type it in the future). A brief message appears while pgAdmin is
establishing a connection. When you’re connected, several new object items
should display under the server name.
. Expand Databases and then expand the default database postgres.
. Under postgres, expand the Schemas object, and then expand public.
Your object browser pane should look similar to Figure 1-4.
Figure 1-4: The pgAdmin object browser
NOTE
If pgAdmin doesn’t show a default under Servers, you’ll need to add
it. Right-click Servers, and click Create▶Server. In the dialog, type
a name for your server in the General tab. On the Connection tab,
in the Host name/address box, enter localhost. Fill in your
username and the password you supplied when installing
PostgreSQL and then click Save. You should now see your server
listed.
This collection of objects defines every feature of your database server.
That includes tables, where we store data. To view a table’s structure or
perform actions on it with pgAdmin, you can access the table here. In
Chapter 2, you’ll use this browser to create a new database and leave the
default postgres as is.
Exploring the Query Tool
The pgAdmin app includes a Query Tool, which is where you write and
execute code. To open the Query Tool, in pgAdmin’s object browser, first
click once on any database to highlight it. For example, click the postgres
database and then select Tools▶Query Tool. You’ll see three panes: a
Query Editor, a Scratch Pad for holding code snippets while you work, and
a Data Output pane that displays query results. You can open multiple tabs
to connect to and write queries for different databases or just to organize
your code the way you would like. To open another tab, click a database in
the object browser and open the Query Tool again via the menu.
Let’s run a simple query and see its output, using the statement in Listing
1-1 that returns the version of PostgreSQL you’ve installed. This code,
along with all the examples in this book, is available for download via the
resources at https://nostarch.com/practical-sql-2nd-edition/ by clicking the
link Download the code from GitHub.
SELECT version();
Listing 1-1: Checking your PostgreSQL version
Enter the code into the Query Editor or, if you downloaded the book’s
code from GitHub, click the Open File icon on the pgAdmin toolbar and
navigate to the folder where you saved the code to open the file
Chapter_01.sql in the Chapter_01 folder. To execute the statement,
highlight the line beginning with SELECT and click the Execute/Refresh
icon in the toolbar (it’s shaped like a play button). PostgreSQL should
return the server’s version as a result in the pgAdmin Data Output pane, as
in Figure 1-5 (you may need to expand the column in the Data Output pane
by clicking on the right edge and dragging to your right to see the full
results).
You’ll learn much more about queries later in the book, but for now all
you need to know is that this query uses a PostgreSQL-specific function
called version() to retrieve the version information for the server. In my
case, the output shows that I’m running PostgreSQL 13.3, and it provides
additional specifics on the build of the software.
NOTE
Most of the sample code files you can download from GitHub
contain more than one query. To run just one query at a time, first
highlight the code for that query and then click Execute/Refresh.
Random documents with unrelated
content Scribd suggests to you:
Life is just one breathless bustle now-a-days. Hardly had we got
our minds adjusted to the engineers when a whole battalion of
machine-gunners marched into town. From the moment they arrived
it has been one interminable line from morning until night,
demanding the Three C.s,—chocolate, cookies, and cigarettes.
Luckily my closet was well stocked and so has stood the strain.
And speaking of closets, I have acquired a skeleton in mine. It
came about through a sick soldier, an accommodating captain and
an egg-nogg. The sick boy I discovered in Madame the Caretaker’s
stable while breakfasting this morning. He was very miserable,
Madame told me, and had been quite unable to eat a thing for days.
I stopped in at the stable and verified her words. The boy looked
wretched.
“Come to the canteen at ten o’clock and I’ll have something for
you to eat,” I told him. Then I begged a cup of fresh milk from
Madame.
The Captain I discovered in front of my canteen counter, and
knowing him to be a southerner and a gentleman, I summoned my
courage and whispered a petition for a few drops of something, from
the flask he carried in his pocket, to put in the egg-nogg for the sick
boy. The Captain, who was corpulent and dignified, in some
embarrassment replied that he was unfortunately without anything
at present, but that the lack would be immediately supplied. He
disappeared, returning to produce before my startled eyes, from
beneath his coat, a life-sized bottle labeled cognac. Then he invited
himself into the kitchen to help make the egg-nogg. He proved
expert. I quaked fearing the customers would sniff the cognac
through the lattice-work. The sick boy came, turned out to be one of
the Captain’s own men. The Captain cocked an unsympathetic eye.
“What’s the matter with you, Smith?” he questioned, “been drunk
again?”
“Captain,” I scolded horrified, “I won’t have any rough talk like
that in my kitchen!”
Smith indignantly denied the charge. He drank his egg-nogg and
left looking three shades happier.
“Captain,” said I, “did you ever make an egg-nogg for one of your
men before?”
“Never,” replied the Captain with decision. He drained his own
bowl and took his departure. “I will leave the bottle behind,” he told
me.
“But I don’t want it!”
“You might need it again,” he declared. And nothing could induce
him to change his mind.
That bottle weighs on my conscience like a crime. I have hidden
the guilty thing in a corner of the store-room shelf behind some
perfectly innocent-looking bundles of stationery and a pile of safety
razor blades. But out of sight it continues to haunt my mind. I feel
as if I were giving sanctuary to the devil. And, worst of all, I have a
vision of coming into the hut some day to find that the bottle has
been discovered and the whole Y. M. C. A. is on a jag.
Mauvages, November 11.
It isn’t true. It isn’t real. It can’t be that the war is really ended.
This morning I awoke to the sound of the most tremendous
barrage I have ever heard. At this distance however it was almost
more like a sensation than a sound, a sort of incessant thrilling,
throbbing vibration.
The question was on everybody’s lips: “Do you suppose they
really will sign the armistice?” “It don’t sound much like peace this
morning!” would come the dubious reply. We have heard rumours
just since yesterday, but in rumours we have so long ceased to put
any faith! As the morning wore on our skepticism grew. The almost
unbroken reverberation frayed the nerves. As eleven o’clock drew
near the tension became torture. Would the guns cease? Could
they? It seemed as if they must go on forever. The clock in the old
grey church tower began to strike the hour. I flung open the kitchen
door. We all stood breathless, frozen, listening. Ding-dong, ding-
dong; through the notes of the bell we could still hear the throbbing
of the great guns. Eleven times the slow bell chimed, there was a
heavy boom, one more, and then absolute silence. We stared at
each other blankly incredulous. “They’ve signed,” said a boy.
I walked down the little lane that leads to the ammunition dump
and picked a bunch of orange-scarlet berries. I wanted to be alone,
to listen. It was a day all pearl and lavender, a violet mist hung over
the brown hill-sides. No one passed on the road, there was not a
sound of any sort that reached me, the world seemed to be asleep.
The stillness was terrifying. I waited, tense, not able to believe,
expecting every moment to have the silence broken by the
resumption of the cannonade. Then as the minutes passed and still
my strained ears could not catch so much as a whisper, I turned
back and entered the little roadside Chapel in the Bush. There in its
dim blue and silver solitude I knelt down before the little statue of
Jeanne d’Arc and prayed.
At noon someone started the old church bell to ringing, it jangled
frantically for hours.
I think we are all a little dazed. I for one have a curious feeling as
if I had come up suddenly against a blank wall.
Mauvages, November 12.
Last night we celebrated. The whole ordnance camp got out and
set off flares and signal rockets from the dump, while two of the
boys put over a barrage with the machine-gun on the hill. And there
was much champagne. This morning the street is hung with flags,—I
never knew before how thrilling the tricolor could be until I saw it
like this, against the stone-grey of the old houses.
A company of French cavalry is just passing through town. They
are very beautiful to look at, with their bright blue uniforms, their
bright bay horses, and the long slim lances which they carry in one
hand, each with a tiny pennant at the end. As each one comes into
view down the street I think; “Thank God, for one more Frenchman
left alive.”
The boys have already begun to argue about the date on which
they will reach home. But though the fighting may be over, there are
long months still ahead of us here I am sure. And now with the
strain and the excitement gone, France is bound to look greyer and
muddier and more whats-the-use to the boys than ever before. May
Heaven help us all!
Mauvages, November 17.
I want to make you acquainted with Bill and Nick, my two
invaluable assistants. Bill is my official detail formally assigned. Nick
is a volunteer, his services a free-will offering proferred at such times
as he is not required in his regular capacity as guardian of the bath-
house.
Bill is a lame tame giant six feet two and up. He slipped a cog in
his knee one time while shuffling shells last summer and never got
quite straightened out again. Bill is my salvation. He redeems what
would otherwise be a desperate situation. For Bill has a Business
Brain. If it weren’t for that, I believe I should be driven to the mad-
house trying to balance the francs and centimes at the end of each
week. Besides having a head for figures, Bill is an all round handy
man with a turn for inventions. When I come back to the hut after a
morning expedition to Gondrecourt in quest of suppplies, I may or I
may not find last night’s dishes washed but I am pretty sure to find
some wonderful new contrivance added to my hut equipment. Bill
has made me a stove-pipe out of a German powder can. Bill has
installed an automatic closing attachment for the main door, which
consists of a rope, a pulley, a stove grate and an excruciating
squeak; the chief advantage of this invention being the squeak
which always betrays the sneak who tries to escape undetected in
the middle of a prayer. Sometimes I think it hurts Bill’s pride to have
to take orders from a lady, especially one with such an
unmathematical brain as I. Occasionally he lapses into a you’re-only-
a-little-girl-after-all sort of attitude and then I have to put on all my
dignity and read the riot act to him. But when I hand in my weekly
cash sheets at Headquarters and the cashier there tells me that my
accounts are the best in the whole area, why Bill could have the
whole hut and everything in it.
As for Nick, if Bill is right hand man, why Nick makes a quite
indispensable left, and this in spite of the fact that the poor fellow is
almost blind. He got a crack in the back of his head from the corner
of a case of “75s,” while unloading ammunition some two months
ago, which affected the optic nerve. And though the doctor promises
a partial restoration of his sight, at present he must grope about in
dark glasses and semi-darkness. Nick has a history. An orphan,
educated for the priesthood, he ran away at the age of sixteen and
started on the career of a cowboy. After having broken every bone in
his body in the course of his broncho-busting he rose to the heights
of his profession and joined Buffalo Bill’s Wild West Show. Here he
met his wife, a lasso and pistol expert. While riding an “outlaw” in
Madison Square Garden, he was thrown and had one of his legs
badly smashed, which forced him to retire from public life. After this
he spent a couple of years as a bar-tender in New York. In his spare
moments, aided by his ecclesiastical Latin, he learned practical
chemistry from an old German druggist who kept shop next door.
Now in his civilian capacity Nick is consulting chemist for a Brooklyn
laundry concern, while his wife conducts successfully a French
millinery store in Flatbush. So much for romance!
Nick is, I am quite sure, the politest Irishman in France. Moreover
he is the darling of the feminine portion of the town. Partly by
reason of his blindness, which appeals to the quick sympathies of
the Frenchwomen, and partly because of his unvarying courtesy, his
kindliness and his quaint humour, he is the most sought-after man in
Mauvages. He knows, I should judge, some six words of French, but
with these he manages to “get by.” And he is forever being invited
out to supper.
Every morning between sweeping up and washing the dishes and
waiting on the counter we hold a coffee party in the kitchen; Bill and
Nick and myself and whoever else happens to be around. The party
consists of coffee with plenty of sugar and canned milk,—always a
treat in the army as in the messes you must drink it plain;—and K. P.
cookies. Now K. P. cookies, you must understand, are cookies from
the end of the package that the mouse didn’t eat. As there is
considerable activity on the part of the mice these days there are
any number of K. P. cookies. And yet I have done my best. Pricked
on by conscience I said to Nick day before yesterday, “Nick do you
suppose you could get me a trap?”
“Certainly Ma’am, I’ll buy one at the store.”
“But wait a minute, do you know the word for mouse-trap?”
“Don’t worry. That’s not in the least necessary.” And he set out for
the General Store Articles Militaire down the street.
But for once his sign language failed him. He was offered
everything in the store from a screw-driver to an egg-beater and
only achieved the trap finally by stumbling over one on the floor. It
was a French trap to be baited with flour and sewed up with thread;
I looked at it skeptically, but the next morning we had caught a
mouse. However today it was K. P. cookies as usual.
“Bill,” I said, “you’ll have to borrow Iodine.” Iodine is the Medical
Sergeant’s cat.
“Aw shucks,” says Bill, “Iodine is a frog cat. She wouldn’t look at a
mouse unless you served it to her on a platter dressed with garlic.”
Bill says no home is complete without a dog. I quite agree with
him. Only, I say, we must catch him young so we can bring him up
in the way he should go. These French dogs for the most part seem
to have neither manners nor morals. So Bill is keeping an eye out for
a likely puppy.
“But,” he said, “when we close up here, the only way we’ll be able
to settle it between us will be to make him into sausages.”
If we ever do get a dog I think I shall call him “Tin Hat” just
because every other dog in the A. E. F. is named “Cognac.”
Mauvages, November 20.
Our relations to the French populace are enough to try a
diplomat. Hardly a day passes in the hut but what some delicate
social or ethical problem arises.
First, there is Louis, a most disreputable old scamp if there ever
was one. He keeps the café across the street and so is my deadly
rival. The other day the old rascal appeared at my counter grinning
from ear to ear, and demanded “bonbons pour le rheum,” producing,
in witness of his urgent need, a feeble and patently artificial cough.
When I answered that unfortunately we had none, he instantly
substituted chocolate in his request. Unable to resist the rapscallion’s
grin I gave him a handful, whereat in beaming gratitude he
immediately invited me over to the café to have a glass of wine at
his expense. And when I hastily informed him that I didn’t care for
wine he genially amended the invitation so that it stood, “glass of
beer.” And now I am told by the boys that he has announced that I,
forsooth, am his “fiancée!”
But chiefly there is Rebecca. We call her Rebecca because when
Bill goes to the well to get a pail of water he usually happens to
meet her there. Rebecca is thin and dark and lively. Her English
vocabulary includes such phrases as “beeg steef” and “Mek eet
snappee!” She is, as the boys put it, “full of pep.” Rebecca has a little
black and villainous-looking husband who occasionally appears in
town from the trenches, but for the most part she is free to follow
where her fancy leads. If it should ever lead her to confession I am
afraid she would make the old Curé’s eyebrows curl.
Bill’s acquaintance with Rebecca is entirely on business lines he
wants me to understand. She does his laundry for him. “It’s all very
well,” I say, “to take her your washing, but why must you take her
chocolates?” He knows I disapprove. When he lingers too long on
the water detail I eye him severely on his return.
“Bill, have you been hobnobbing with Rebecca?”
Bill grins admission.
Rebecca lives in a white little one story door-and-window house
just around the corner on the Rue d’Eglise which I must pass going
to my canteen. And Rebecca keeps tab on the precise hour and
minute at which I return to my billet under Bill’s escort every night.
Going home one stormy night I took Bill’s arm. The next day Bill
informed me that Rebecca had advised him that such conduct,
according to French notions, was not quite comme il faut.
Bill, I find, is able to make an astonishing amount of conversation
with his “nigger French” that takes absolutely no account of moods,
tenses, conjugations, declinations or any of the other stuff in
grammar books. And I am afraid he understands a great deal that it
would be just as well he didn’t.
“How did you learn it all?” I asked him.
He looked at me side-wise. “Rebecca gave me lessons,” he
answered grinning.
Last night, as we passed Rebecca’s house, I noticed that her door
was the least bit ajar.
As Bill left me at my gate I admonished him; “Now don’t you stop
to say good-night to Rebecca.”
“Gosh, no!” said Bill, “if I did I’m afraid I might have to hurry or
I’d be late for breakfast.”
Whenever I meet Rebecca on the street she always bows to me
most urbanely.
Nor is Rebecca all my concern in relation to Big Bill. There is also
the pretty girl who lives down the street who undoubtedly would not
be averse to accompanying him to America. Bill stops at her house
every night in order to get a quart of fresh milk for the C. O.’s
breakfast. I bid him be wary of these Franco-American alliances,
citing horrible examples I have known, such as the machine-gunner,
for instance, who, in order to be in harmony with his future family-
in-law, felt it incumbent on him to appear at his wedding wearing a
pair of wooden shoes; and of the doughboy who married a widow
with two children, and, since he knew no French and she no English,
persuaded his company commander to detail an interpreter to live in
the house with them for the first three days after their marriage.
Not many days ago a girl came to my kitchen door in company
with a soldier. She had a United States paymaster’s cheque which
she wished to have cashed. Afterwards I questioned Bill. It seems a
lieutenant had married and afterwards divorced her. She was still
drawing his allotment. She looked so thoroughly the peasant, bare-
headed, in a shawl and shoddy skirt, with nothing to particularly
distinguish her pretty but inexpressive face, that I voiced my wonder
to the boys.
“Oh but you ought to see her when she gets dressed up!” they
said.
“Fine feathers don’t make fine birds,” I remind severely. “Bill, be
warned!”
“Yes, but there’s Gaby,” Bill suggests. “What about her?” Now
Gaby is the little chauffeuse who has been driver for a French
general three years and who turns up periodically in town. She is
quaint as a wood-cut and solemn as an owl, with her shock of
bobbed hair and her great staring child-like eyes. She sits at the
mess table and never says a word but draws your glance irresistibly.
Always she wears an odd little straight-cut dress hanging just below
her knees and a croix de guerre pinned to her breast. Gaby killed a
man with her car not long since and was held a prisoner at Ligny-en-
Barrois for ten days in consequence. Gaby and one of the sergeants
at the A. R. are undergoing all the woe and wonder of love’s young
dream.
“Oh well,” I say, “Gaby is different.”
This afternoon Rebecca appeared at the canteen and asked for
Bill. She was so elegantly attired that at first I didn’t know her. After
a parley at the door, Bill, with an odd expression on his face, takes
his second-best raincoat from the peg and hands it to her. I looked
my inquiries. An old doughboy sweetheart of the lady’s, it appears,
had returned on leave and they were going travelling together.
“Going off on a honey-moon with another feller, in my raincoat!
Gosh, it’s a cruel war!” grinned Bill.
Mauvages, November 24.
Now that the time is drawing on toward Christmas the boys,—
bless them!—are all wanting to send some remembrance to
mothers, sisters, wives and sweethearts at home. But what to send
has been the desperate question. One sort of goods and one only is
offered for such purposes by the French stores in this locality, a line
of flimsy silk stuff, handkerchiefs, scarfs and little aprons, machine-
embroidered with gay flowers and each bearing the legend
“Souvenir de France.” They are fragile slazy things, absurdly high-
priced, inappropriate and often hideous. But to the boys they are
altogether beautiful. After many requests and inquiries I gave in. I
went to Gondrecourt and purchased what I could find that was the
least tawdry, the least exorbitant. I brought them to the canteen;
they proved so popular that three days afterward I had to make
another trip to town to buy some more. Now we carry a regular
stock of fancy silk handkerchiefs and aprons in addition to the
chewing tobacco and cigarettes. But here one is faced with a
delicate problem. Each handkerchief is embroidered with some such
specific legend as To my Sweetheart, To My Dear Wife, To my
Darling Daughter,—I refused to consider the bit of lacy frippery
marked To my Dear Son!—and this complicates matters immensely I
find. Somehow we always manage to have a supply of Sweethearts
on hand when a man is in quest of a Dear Wife and vice versa. In
vain I artfully suggest that it would be a pretty compliment to call
one’s wife “Dear Sweetheart,” to their minds there seems to be
something essentially compromising in such a notion. Occasionally
the reverse will work however, and a boy, grinning and abashed, will
select a handkerchief marked “Dear Wife” to send to his sweetheart.
Sometimes during these sales one’s faith in the single heartedness of
Young America receives a shock, as when an innocent-looking lad
will blandly select half a dozen “Dear Sweethearts” and put each in a
separate envelope to send to a different girl!
Speaking of souvenirs, there is a boy who acts as fireman on the
dinky little engine that pulls the work-train on the narrow-gauge
between Mauvages and Sauvoy. He belongs to a regiment of
engineers who served with the British in Flanders for some eight
months. While there he dug up enough dead Germans,—“You could
always tell where they were buried because the grass grew so much
greener there,” he explained,—and picked enough gold fillings out of
their teeth, to make a whole match box full. He was going to take it
home and have a dentist put the gold in his teeth “for a souvenir,”
but unluckily in the spring drive he lost all his possessions and the
match box with them. Now this, as Kipling would say, is a true story.
Mauvages, November 30.
Let me recount to you the gentle tale of the German prisoners
and the Thanksgiving movies, an incident which I consider a sort of
sermon in a nutshell and a Warning to the Nations.
Unluckily there is in this division a secretary who is a
sentimentalist. He has an idea that an important part of his object in
France is “to enliven the long evenings of the French villagers,” and
particularly does he consider it his Christian duty to do something to
demonstrate how much we love the poor German prisoners, those
gentlemen who wear the big P. G. for Prisonnier de Guerre on their
backs and “ought,” as the boys say, “to have an I in the middle.”
There are several hundred of them in a camp at Gondrecourt and
they are, it is said, just as well housed and fed as our boys, and not
made to work nearly as hard.
Now, as there was no other sort of entertainment available, I had
set my heart on having movies in my hut on Thanksgiving. I had
presented my request at the Headquarters office and understood the
matter settled. But the Sentimental Secretary it seems had made up
his mind that the poor dear German prisoners must have a treat
and, other schemes falling through, he also put in a request for the
movies. There was only one portable machine in working order.
Through some misunderstanding or something in the office, the P.
G.s got the movies. To enlarge upon my sentiments when the news
was broken to me Thursday morning or to record the opinions
expressed by the boys in regard to the matter, is not to the purpose
of this tale.
Failing our show, all that I could manage in the way of celebration
was a little box of nuts and raisins tied up with a bit of red, white
and blue ribbon for every man in camp. The mess sergeant,
however, outdid himself. Our Thanksgiving dinner was nothing less
than a feast. For days the A. R. jitney had been scouring the country
for poultry. At last the sergeant had succeeded in getting enough for
all. He did this by assembling specimens of the whole feathered
tribe; turkey, duck, chicken and goose. And I had a slice of each. But
for all that I didn’t enjoy that dinner worth six-pence. Those movies
were on my mind. I tried to think of the touching gratitude of the
German prisoners. Perhaps after all if one should pursue them with
delicate attentions it might lead them to see the error of their ways.
Perhaps giving them a movie show would inculcate, by example, a
beautiful lesson of Christian charity and forgiveness. Who could tell
what uplifting moral influence Charlie Chaplin or Mutt and Jeff might
exert?
Last night was our regular movie-night. In the midst of preparing
for the show, Georges, the French operator, who was getting the
machine ready, Georges the little dandy, always nonchalant and
blasé, came charging back to the counter, his eyes as big as arc-
lights. He thrust his hands, which were full of cartridges, beneath my
nose, fairly dancing on tip-toe in his excitement. He had found them
in the carbide; when the carbide had gotten hot, “Poof!” he
dramatized the wrecking of the hut with explosive gestures. “C’est
les Boches! Les cochons!” Never again would he take his machine
there, never, never!
As the machine had been left at the German Prison Camp after
the Thanksgiving show and then brought directly from there to
Mauvages there seems little room for doubt that the prisoners had
placed the shells there. Of course, if there were any poetic justice in
things, the Sentimental Secretary himself would have been blown up
by the Germans’ cartridges, but unfortunately in real life things don’t
happen that way.
Mauvages, December 3.
The French Army is in possession of Mauvages. A regiment of
artillery moved in on us yesterday afternoon. There seemed a never-
ending line of them as they crawled into town, the horses just barely
able to drag the heavy pieces. There must have been a shocking
shortage of fodder in the French Army; the poor beasts look
wretched beyond words. The big guns are lined up all along the
street. They look like great spotted lizards in their green and brown
and yellow coats of camouflage. Each piece has a girl’s name carved
on the muzzle. The one in front of my canteen is Marthe, further up
the street stand Lucile and Marie. We watched them as they brought
the guns into place, unhitched the teams and made their
preparations to settle down and stay. Once settled, our perplexities
began. Immediately they started to trickle into the canteen in search
of cigarettes. To the first comers in a weak moment I slipped a few
packages. That was enough. Thereafter it was just like flies to the
molasses jar, and then of course I had to harden my heart and say
no. But they wouldn’t take no for an answer. They begged, pleaded
and cajoled. I posted a polite sign at the end of the counter
explaining how the canteen supplies had been brought into France
without payment of any duty, under the strict agreement with the
French Government that they would be sold only to Americans. But
they refused to read the sign. One handsome brigadier stopped me
on the street in order to present his petition. And at the canteen a
little poilu with a round cherubic face, after being refused some nine
or ten times over at the counter, followed me out into the kitchen to
urge his piteous plea. It was dreadful, it was harrowing. I have
never felt quite so mean about anything in all my life.
In the evening we had billed a stereoptican lecture on London.
Forseeing that the poilus would form a large proportion of the
audience, I tried to get an interpreter to explain the pictures in
French to them but at the last minute the interpreter failed me.
Notwithstanding, the Frenchmen remained courteously quiet while
the lecture lasted. But once it was finished the atmosphere of the
hut underwent a change. The blue-coated figures who were
swarming into the canteen now had evidently spent the earlier part
of the evening in the cafés. I went out into the centre of the hut to
see what was going on; all about me stretched a swarm of poilus in
a genial mood. The door squeaked open, a little soldier came
skipping into the hut. To my horror I saw he carried in one hand a
tall tumbler and in the other a large bottle of Benedictine. The
victrola was jigging out a rag on the counter. Posing for a minute in
an attitude reminiscent of the great Isadora, the little poilu
proceeded to dance in time to the music, pirouetting on one toe as
he waved the bottle and the tumbler above his head with
Bacchanalian gestures. Then suddenly he sat down at one of the
tables and started to pour himself a glass. I swooped down upon
him. It was défendu I explained, strictly and absolutely défendu to
drink in this hut. He stared incredulous. I reiterated with emphasis.
Finally he nodded sulkily and, slipping the bottle underneath his arm,
turned away. Two minutes later I caught him offering a red-nosed
friend a drink square in front of my counter. I flew to the attack
again. I told him it was against the rules to so much as bring wine
into the hut. He held his ground defiantly. I wanted to take the little
wretch by his coat collar and march him out the door; I felt I could
have done it. Instead I plead, expostulated and commanded. A score
of grinning poilus crowded about us: it was evidently as good as a
show to them. I entreated the little poilu please, please to carry the
bottle out of the hut! “Dehors! Dehors! Outside!” they chorused
gleefully. I exhausted my vocabulary, apparently without effect. The
little poilu wasn’t used to taking orders from a girl, especially one
who spoke French so badly, but finally I won. “Bon!” he snapped
explosively, turned on his heel and marched out. I fled precipitately
to the kitchen and stayed there until closing time. I didn’t feel equal
to coping with any more tipsy poilus.
It’s curious how the whole character of a dwelling-place can
change. When the priest and the cat and I are keeping house
together, the old mansion is the dimmest, most decorous place
imaginable. At night I let myself in the dark front door, locking it
carefully behind me,—Monsieur scolded me for leaving it unlocked
once; I had left him, he said, at the mercy of the passersby!—then
grope my way down the cold unlighted hall and up the steep stairs
to my chilly room and to bed by one flickering candle’s light. The
place is as silent and lifeless as a tomb. Then new troops come into
town and suddenly everything is changed. The lower floor is taken
over for an officers’ mess and often too, for Headquarters. Savory
odors of cooking, warm smells mount up the dim stairway, candles
gutter in niches in the passage-ways, smart-looking officers in khaki
or horizon-blue as the case may be, meet and salute one in the hall.
The tramp of booted feet, the ring of spurs, the clink of glasses,
laughter, song, the piano played tumultuously sometimes late into
the night,—everything from Madelon to Mozart—and most startling,
and incredible of all, the jangle of a telephone bell, installed for the
occasion; for a few days we live in a strange bustling vivid world,
then on they move and we are left again to our silence and solitude.
Tonight as I was washing up for supper I was startled by a rap on
my door. There stood Monsieur le Curé and a French officer. I had a
bad moment wondering what the cause of such a visitation might
be. Was he going to turn me out of my billet perhaps? Or was he
going to complain about the treatment his men had received in the
Y.? Monsieur le Curé was ambling through a long and elaborate
peroration. At first I could make no sense out of it, then suddenly I
caught on. Monsieur le Capitaine was a stamp collector. He wanted
to know if I perhaps had some stamps des États-Unis which I could
spare him!
Reports have come in tonight of friction between the French and
American soldiers in town, resulting in a number of scrimmages. The
whole trouble springs, I gather, from the eternal feminine and the
native jealousy of the male; the Fair Sex of Mauvages having made
quite evident to the poilus their decided preference for the
doughboys.
Mauvages, December 6.
The theatrical season at Mauvages has been inaugurated. The
carpenters were busy in the hut all day yesterday, hammering and
sawing, making us a roll curtain out of roofing paper, manufacturing
foot-lights from commissary candles and tin reflectors cut from the
lining of tobacco cases. When the stage was done it was very gay.
We had a red curtain across the back, bright yellow wings, red and
yellow draperies around the proscenium arch, festoons of little flags
strung across the top, and a large American flag draped centre back.
It wasn’t what we wanted, it was just what, by hook or crook we
could get, and the effect really wasn’t half as bad as it sounds.
The programme might be classed in two parts, rehearsed and
impromptu. For a starter we dropped a tear over Baby’s Prayer, that
bit of ninety-nine one-hundredths pure sentimentality, without which
no programme in the A. E. F. is complete these days; after which we
were adjured to “Pray for sunshine, But always be prepared for
rain,”—a quite superfluous admonition in this part of France at this
season of the year!
“Put all your pennies on the shelf,
The almighty dollar will take care of itself.”
“Humph!” grunted the boy next me, “I’ll bet it was a Jew wrote
that.”
Following the songs we heard Barney, the Poet Laureate of the
Camp, celebrate the deeds of the ordnance detachment in verse. At
least we supposed that was what it was, for Barney has a brogue all
his own and if you get one word in ten you’re lucky. As the C. O.
says, it is much easier to “compree” a Frenchman than it is to
understand Barney.
After Barney we had a sermon, a burlesque darky sermon
preached by a black-face comedian. As luck would have it, two real
darkies from a labor camp up the line slipped in at the back of the
hut just as the preacher began. They took it all in deadly earnest,
and warmed, I suspect, by a glass at the corner café, they presently
began to respond to the preacher’s exhortations with genuine
religious fervor.
“Dat’s so! You tell ’em bruder! Hallelujah! Bless de Lord!”
The audience up front, hearing a commotion and unluckily not
catching the comedy, hissed indignantly and the darkies, abashed,
slunk out.
Of course at the last moment some of our headliners failed to
come across. The mumps claimed our dramatic reader and our buck-
and-wing dancer sent word, just as the curtain was going up, that in
all the camp, no shoes outside of hob-nails, large enough for him
could be found. But we made up for these defections by our
impromptu acts. The most surprising of these was the Little Fat
Poilu. He popped up suddenly from Heaven knows where, a round
rosy dumpling of a man with a shiny nose and a fat black beard, and
offered his services. On his first appearance he played the violin with
vim and spirit. Then in answer to the applause he dropped his violin,
seized the tall hat from the head of the darky preacher, clapped it on
his own, and bounced back onto the stage. The transformation was
amazing. In an instant, instead of a poilu he had become a jolly little
bourgeois shopkeeper out for a stroll on the boulevard. He
proceeded to sing a comic song, a song with an interminable
number of verses, unquestionably very funny and in all probability
quite scandalous. The French portion of the audience was charmed,
they joined vociferously in the jiggy choruses, and when he had
done they insisted on another and another. For a while it looked as if
France was going to run away with the programme, but finally the
little poilu came to the end of his repertoire,—or of his breath
maybe, and America once more took the stage.
Today we are living in an atmosphere of theatrical enterprise.
Already there are three or four “bigger and better” rival shows in
process of incubation. What’s more, Barney is writing a play. He sits
at one of the canteen tables surrounded by a group of admiring
would-be actors and each sheet, as he finishes it, is gravely handed
around the crowd. So far it seems to contain just three characters;
Rose the beautiful stenographer, the villain landlord and the office
boy. I am waiting in suspense to see whether Barney’s masterpiece
is going to turn out a melodrama, a problem play or a dramatic
treatise on the social and political wrongs of Ireland.
The French troops are moving tomorrow. Tonight the Little Fat
Poilu came to bid us good-bye. When no one was looking I filled his
pockets up with cigarettes.
Mauvages, December 9.
A very regrettable incident occurred last night. The day being
Sunday we were due for a religious service at seven-fifteen. At
seven-ten the Reverend Gentleman, who was to instruct my flock in
the way wherein they should go, arrived in company with the
Business Manager from Gondrecourt. Now it happened that the
Reverend Gentleman on this occasion was none other than my friend
the Sentimental Secretary. He surveyed the congregation; there
were nine boys in the hut. He sat down and waited for the audience
to arrive. But the audience didn’t. Instead one wretch surreptitiously
sneaked out the door. At last I felt it necessary to come forward with
apologies and explanations; my flock at present was small to start
with, the sheep had all gone to Domremy on an excursion, the goats
were deep in an after-payday poker game.
“Do you wish me to hold the meeting?” the R. G. questioned
grimly.
“If you will.”
The Reverend Gentleman, a bit tight about the lips, laid on. It was
a cold night; we gathered by the fire. I tried to make myself look as
large as possible, but stretch the congregation as you might, we only
reached two-thirds of the way around the stove.
“Well,” said the Business Manager when it was all over with, “how
soon will you be ready to close out this hut?”
I reminded him that after all it would have only taken ten
righteous to save Sodom, so might not eight save Mauvages?
Of course just as soon as the Reverend Gentleman and the
Business Manager had shaken our dust off their feet and
disappeared, a whole crowd of boys came streaming into the hut. I
accused them of having waited just around the corner until they had
seen the Religious Service depart. As for Big Bill I consider him
nothing short of a slacker, he sat in the kitchen all evening and
wrote a letter to his girl. I tell him that as hut detail it is obviously
his duty to attend all services but he explains that “it makes him
homesick.”
In a town on the road between Mauvages and Gondrecourt there
is a labor camp of Chinese coolies. These are the laziest folk in
Europe I am sure. They are supposed to be working on the road,
which needs it badly enough, resembling, as one boy declared, “the
top of a stove when all the lids are taken off.” All day long they squat
by the roadside, or stand idle watching the traffic go by. “They’d
rather be caught dead than caught working,” as one boy said. The
story goes that if one of them dies the French Government must pay
the Chinese Government thirty francs. They come dear at that.
Moreover, they are unconscionable thieves. Up on the hill back of the
town where they are billeted there is an American aviation field. The
camp was abandoned after the armistice, but twelve boys from the
air service were detailed to stay and guard the property. These boys
find that the chief end of their life is to chase the Chinks out of the
stores; they are quite persistent and perfectly unabashed. More than
that, if the Chinks catch one of the guards by himself, they are likely
to attack in force armed with sticks and as our boys are not allowed
to carry weapons, such an attack is no laughing matter. The trouble
began, the boys tell me, in the days when the camp was populated;
two mechanics had once thought it a good joke to give one of the
Chinks a bath by ducking him in the horse-trough.
One of these heathen, I am told, came to church here at
Mauvages yesterday and almost broke up the meeting. It pleased
him to sing all the way through the service, a wierd sing-song chant
all his own, and as if that were not bad enough, in the middle of a
prayer he had turned square about and started to play with the
rosary of the scandalized Madame behind him! The most pious-
minded could scarcely keep their thoughts on the priest’s
dissertation. There was “beaucoup distraction” as one Mademoiselle
phrased it.
This morning I went down to Gondrecourt.
“Well, and how are your eight men?” asked the Business Manager.
“One of them has gone to the hospital with the mumps,” I
answered. “So now I have seven.”
Mauvages, December 12.
I have been A. W. O. L. I have been on a joy ride. For the first
time since I came to France I have taken a real day off. I got a
chance to go up to the old battle front on a “speeder.” I didn’t
mention the matter to the office, but I took the chance. I knew I
could safely trust the hut to the management of Bill and Nick for one
day.
We started out shortly after six A. M., on the narrow-gauge bound
for Mont Sec. There were five of us on the speeder which is, you
must know, a little flat car something like a hand-car, only that
instead of being propelled by hand power, it is run by a gasolene
motor. Speeders are the jolliest possible way of travelling and they
can go like the wind: they possess just two disadvantages, their
propensity for having engine trouble, and the ease with which they
jump the track at the slightest provocation. It is told how in
Abainville the other day a speeder jumped the rails, the engineer,
after turning a half a dozen somersaults, picked himself up, squared
off, demanded; “Who in hell put the pebble on the track?”
From Mauvages we followed the A. and S. to Sorcy. There we
switched onto the line which the boys at Abainville used to declare
“ran through the trenches.” They would tell me wonderful tales of
the trips they had taken on this line; the smoke-stack of the engine
protruded over the top, they explained, and “Gosh, you could hear
the bullets just splatterin’ against it!”
A short ways out from Sorcy we passed the last inhabited village.
Ahead of us we could see the barren sinister outline of Mont Sec,
that little Gibraltar of the land which the Germans had captured and
fortified early in the war, which the French had endeavored to retake
in 1915 with the most fearful losses, but which had remained
impregnable, commanding, looking down in contempt on our men in
their muddy lowland trenches of the Toul Sector, until, on September
twelfth, the American Army had taken it along with the rest of the
Saint Mihiel salient.
As we neared Mont Sec we began to pass devastated villages,
some of them mere formless ruins, others from a distance holding
the shape and outline of habitable dwelling-places but on approach
revealing themselves as mere groups of riddled house-shells. Across
the open places stretched interminable grey swathes of rusting
tangled wire, “barbed-wire enough to fence Texas,” as one boy put
it. On sidings we passed long lines of cars full of salvage, all the junk
of war tossed carelessly together. Along the tracks were scattered
empty shells and here and there piles of unexploded ammunition. In
a shell-hole by the roadside, half filled with water, lay a hob-nailed
shoe,—prosaic but pitiful witness of some tragedy. It was the
loneliest land, the most forsaken I have ever seen. Far and wide as
one looked over the empty plain there was no living, moving
creature anywhere.
At the foot of Mont Sec we stopped. There in the woods were the
remains of a German camp; it had been a jolly little place fixed up
like a beer garden underneath the trees, with fancy “rustic” work
and chairs and tables. We left the speeder there, and tramping
across the fields, climbed Mont Sec. Near the top we found the
entrances to the dugouts. The hill was tunneled through from side to
side, all the corridors and rooms walled, roofed and floored with the
heaviest oak lumber. Everywhere through the passage-ways ran a
perfect network of electric wires. Long stairs led to the different
levels. No furnishings were left except the bunks and some rough
tables. We ate our luncheon of bread, jam and corn willy in what
had evidently been the officers’ quarters; the room was nicely
finished with cement, there was a fancy moulded pattern in bas
relief over the doorway, a pipe-hole showed where a stove had
been.
After lunch we inspected the concrete machine-gun pill-boxes
which dotted the hill-top. Then we went down the steep eastern
slope to the village of Mont Sec. About the town, to judge from the
ploughed and pitted vineyards, the fighting must have been the
fiercest. The village was a village of the dead. We went inside the
church; part of the tower, some of the walls, a little of the roof was
left, beyond that nothing. Near the door a French officer had
scrawled “Maudite soit le boche qui détruit les églises,”—cursed be
the Hun who destroys the churches. In this church, Madame the
Caretaker tells me, the Germans commanded all the male
inhabitants of Mont Sec to assemble. Here they were kept prisoners
for three days and nights. On the fourth day they were marched off
at the bayonet’s point into Germany, and no one has ever heard a
word from them since.
Just outside the village in the little cemetery, ploughed with shell-
holes, we found French, American and German graves. The German
inscriptions all commemorated “heroes dead for the Fatherland;” one
of them vowed, with the help of God, vengeance on the enemy.
We went back to the speeder. As it was early in the afternoon we
decided to go on. Rounding Mont Sec, we passed into German
occupied territory. We saw the famous cabbage patches which fed
our soldiers after the Saint Mihiel drive, and, on a hillock beside the
road, one memorable scarecrow dressed from head to foot as a
German soldier, “feldgrau” uniform, cartridge belt, helmet and all. At
Hattonchatel we looked down on the German barracks from the hill-
side but didn’t have time to stop. It was growing late, so we must
turn about-face. Once headed for home our troubles began. The rain
which had been teasing us all day as a faint drizzle, settled down to
business. A few hundred yards down the hill-side the speeder
jumped the track. Fortunately we weren’t running fast and the
speeder jumped on the right side, if it had jumped on the left we
might have gone over the edge of the mountainous hill-side. As it
was no real harm resulted beyond a violent bumping and shaking
up; I jumped and got a lame wrist. “The chances are, that whatever
happens, she won’t turn over,” the boys told me, “so hang on after
this.” So I hung tight. The engine, which had worked like a charm all
the way up, began to sulk and balk by fits. Presently it grew dark.
We had one lantern, we lighted it and the boy who sat at the front
end held it so the light would fall on the rails. Every now and then
the wind would blow it out. At each station along the track we would
stop and ask the engineer operators whether the block ahead was
clear. When we came to the last station before the long forest
stretches about Mont Sec the operator who came out to speak to us
was quite angry; there were three trains, he said, somewhere on the
track ahead; we were doing a very dangerous thing, running after
dark. We went on, straining our eyes as we entered the woods in
order to discern the dark mass on the track ahead which would
mean a train, for the trains, in memory of war days, I suppose, carry
absolutely no lights. A week ago a speeder ran head-on into a train
at night just above Sauvoy; of its three passengers, two were killed,
the other fearfully injured. We held ourselves tense, ready the
moment we had made out a train, and the speeder slowed down, to
jump, and, lifting the car, push it to one side off the tracks until the
train had passed. Once we were lucky enough to make a siding just
at the critical moment. Sometimes we ran at the edge of high
embankments, sometimes we would cross, on a trestle, a wide
marshy stream; then the thought would come to me, What if the
speeder should jump here? And she did jump twice more on the way
back, but luckily both times in well-selected places. The worst
feature of these acrobatics was that the jar had an unhealthy effect
upon the engine and after each occasion the mechanics in the crowd
had to delve and tinker before the speeder could be coaxed to speed
again. Also it was wet. The rain soaked through my raincoat,
through my sweater, into my leather jacket; my skirt was a dripping
rag, the water oozed from my gloves, raindrops dripped from my
nose, my “waterproof” shoes were like sponges. You felt, as one of
the boys put it, exactly like a figure in a fountain.
Between Mont Sec and Sorcy we got a tow. In the dark we came
upon the rear end of a salvage train, tied ourselves up to it, and
bumped merrily along behind until the train turned off on a branch
line and we had to cut loose and make our own way with the
increasingly contrary engine. Fortunately, from that point most of the
way was down hill; on the up-grades we got off and walked; the last
part of the way the boys simply had to push the car. We reached
home at half-past ten, tired, soaked to the skin, but happy.
Mauvages, December 16.
After this, Mauvages is going to be on the map! Mauvages is to be
headquarters for the —— Artillery Brigade, with seventeen hundred
men in town and thousands more in the villages about. Wonderful to
say, this is the very brigade to which my two batteries from the
Artillery School belong and though neither of these will be here in
town, still they will be near enough so I can get a glimpse of my old
boys, I am sure.
Already we have an ammunition train and a crowd of “casuals”
waiting here for their outfits. The hut, which has of late been rather
empty mornings, is now filled all day. These casuals are for the most
part replacements, shipped here directly from the ports, after a ten
days’ residence in France. They have nothing to do at present but sit
in the hut and think how miserable they are. It is funny to hear them
talk. Their opinion of Mauvages is inexpressible in polite terms. They
are quite convinced that they have come to the Very Last Hole on
Earth. In vain I assure them that Mauvages is quite a fine town, as
French towns go, in vain I draw their attention to its beauties and
advantages. They are absolutely certain that nothing could be
worse!
Meanwhile I have been busy making frantic trips into Gondrecourt
to demand, in view of the coming crowds, a new hut, an electric
lighting system, an addition to the old hut, anything or everything,
except a man secretary! But Gondrecourt takes the situation very
calmly.
Just to pass the time away, one of the new arrivals went fishing in
the canal yesterday. He bestowed his catch on me; it measured
about six inches by one and a quarter. As it was still wriggling faintly
I put the poor thing in the water-pail, only to find later that Big Bill
in disgust had thrown water and fish out into the back yard.
Whereupon I raised such an outcry that Bill must go out in the dark
and feel through the wet grass for that fish until he found it. I
carried it down to camp, inviting the K. P.s to prepare it for the C.
O.’s dinner. At dinner it appeared elegantly garnished with parsley in
the center of a huge platter. Just to pay me back they made me eat
it, while the rest dined on steak.
“How do you suppose he caught it?” asked the C. O. I said
nothing. Fishing with hand-grenades is strictly against the law.
Mauvages, December 18.
Mauvages is in disgrace. Mauvages is the black sheep in the Y.
fold. Mauvages is in wrong all the way around. And it’s all because of
one Old Gentleman and his ill-timed opinions.
The Old Gentleman came out to talk to us yesterday evening. We
weren’t expecting him. We were expecting a lecture on the Man
Without a Country,—whoever that may be, Jack Johnson or the
Kaiser! as the boys say,—by the Educational Department. But then
we have almost given up expecting to get what we expect. This is
only the third time we have been fooled on the Man Without a
Country who appears to be our Old Man of the Sea.
The Old Gentleman was brought out in state in the best Y. car by
the Big Chief, the Entertainment Department and a driver. The
Entertainment Department immediately ensconced himself by the
cook-stove with a Sunday Picture Supplement; the driver retired to a
secluded corner to play a game of checkers with one of the boys;
while the Big Chief took his stand out front. I for once back-slid
scandalously, and, instead of occupying a front seat with a deeply
interested expression spread upon my countenance, sat in the
kitchen and ate jam and waffles, the waffles which were heart-
shaped and crisp and heavenly, having been brought by Nick from
his latest supper party.
The Old Gentleman stood out by the stove, the stage proving too
chilly. There was a crowd in the hut. He put his foot in it at the start.
He announced himself as an intimate friend of ex-President
Roosevelt. The boys, sniffing politics, grew suspicious, even hostile.
He began on the scandal of America’s unpreparedness, from that
passed by degrees to the view that Germany was not yet defeated
and as a climax called upon the boys to rise and put themselves on
record as being willing to stay in France until Kingdom come, if
necessary, in order to do the job up brown. The boys did not rise.
Instead they heckled the Old Gentleman until he grew as red as a
turkey-cock and so indignant as to fairly wax speechless. One of the
ammunition train boys, a husky lad who, they tell me, is an old
guard house standby, led the opposition. Out in the kitchen you
could have heard a pin drop. The Entertainment Department and I
sat and stared at each other.
The whole trouble as I saw it, was that the Old Gentleman had
slipped up on his dates. He was giving them a Before-November-
Eleventh speech when it was after the eleventh. It was as if he had
quite failed to comprehend that at eleven o’clock on that date the
whole psychological outlook of the American doughboy underwent
an instantaneous change. His entire mental horizon became
forthwith concentrated to one burning point,—the desire which he
expresses simply but adequately in the words; “I want to go home!”
And not ex-President Roosevelt, nor President Wilson, nor General
Pershing, nor anybody else could make him interested in anything
that was not remotely, at least, related to that issue.
At last the agony was over. The Old Gentleman came back to the
kitchen mopping his brow. When he had finished expressing his
opinion of Mauvages, the driver went out to crank the car. The car
was gone. Of course then, everyone remembered having heard a car
drive off in the middle of the lecture,—every one that is, but I, I had
been too interested in the waffles,—but of course no one had really
thought that it could be, etc. A search party was recruited which
scoured highway and byway. The M. P.s at Gondrecourt were notified
by ’phone. Meanwhile it was ten o’clock, a bleak night and four
indignant gentlemen were stranded six miles from home. An
ambassador was elected to go and lay the case before the A. R. C.
O. The C. O. on his way to bed, instructed the emissary where billets
for the night might possibly be had. But the Old Gentleman, upon
receiving the information, flatly and finally refused to stay in any
billet in town; he would sleep in his own bed or no other. After a
nervous interval the ambassador again approached the C. O., this
time suggesting the loan of his car and chauffeur. The C. O., aroused
a second time from bed, acceeded shortly, the ambassador returned
to despatch the unfortunate Bill to camp to break the news to the
chauffeur. The chauffeur, who was in the midst of an after-hours
poker game, when he recovered from his astonishment, replied
(expurgated) that he’d come when he got good and ready, and
settled back to his game.
In the meantime my four guests by the kitchen-stove discussed in
part the peculiarities of the Japanese language, but chiefly the
shortcomings of Mauvages. The Chief, however, showed himself a
gentleman. He washed the dishes up! And considering that he was a
man and a minister and that the light was dim and the water cold,
he washed them pretty well.
At a quarter to eleven the A. R. chauffeur having presumably
forced all the others into bankruptcy, or gone bankrupt himself,
drove up to the door and I said farewell to my friends.
This morning a rescue expedition was sent out from Gondrecourt.
It finally discovered the lost car, none the worse for its joy-ride, in a
ditch half-way to Sauvoy. Information has reached me on the side
that it was a little group of “hard-boiled guys” from the ammunition
train who stole the auto. They were displeased with the Old
Gentleman’s opinions, and they made up their minds that he should
walk home.
So this is how matters stand: I and my hut are in discredit at
Headquarters, because my boys stole their car. The Old Gentleman
has openly declared that Mauvages is the most unpatriotic spot in
France. The A. R. C. O. is disgusted because he was routed twice
out of bed in one night. The chauffeur is so incensed at me and
mine at having to drive into town at eleven P. M. that he persistently
forgets to stop for my daily papers. And the boys are all sore and
touchy on account of the opinions expressed by the Old Gentleman
in and after his lecture. Such is the happy lot of a hut secretary.
Mauvages, December 23.
The Big Push is here. Our lawn has turned into a gun park with
limbers and caissons elbowing each other under our very eaves. All
day the little hut is crowded to its capacity and at night it becomes
so full that I am literally afraid it will burst out at the seams.
Colonels and captains are forever bobbing up like so many Jack-in-
the-Boxes in my kitchen which I was used to consider as a refuge
and a sanctum. They have the best intentions in the world; they
offer me advice on every subject under the sun from the building of
new shelves in the canteen to the frequency with which I should
require Big Bill to shave. And quite unsolicited they have given me a
detail,—a detail of such proportions that I am swamped. I don’t
know how many there are. They never stand still long enough for
me to count them. Sometimes there appear to be ten and
sometimes twenty. Like the Old Woman who lived in the shoe, I
have so many details I don’t know what to do. They are the nicest
boys that ever were, if only they didn’t take up quite so much room!
Now when I am minded to sit down for a moment to think, my only
course is to go into the store-room and sit on a packing-box, and the
store-room is very cold. And the worst of it is that they all, from
colonel to K. P., have the beautiful idea in their heads that I am not
to do any work, but just to be a sort of parlor ornament, and a
sweet influence; that I will, in short, like the old man who was afraid
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
More than just a book-buying platform, we strive to be a bridge
connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.
Join us on a journey of knowledge exploration, passion nurturing, and
personal growth every day!
ebookbell.com

Practical Sql A Beginners Guide To Storytelling With Data 2nd Edition 2 Converted Anthony Debarros

  • 1.
    Practical Sql ABeginners Guide To Storytelling With Data 2nd Edition 2 Converted Anthony Debarros download https://ebookbell.com/product/practical-sql-a-beginners-guide-to- storytelling-with-data-2nd-edition-2-converted-anthony- debarros-49492608 Explore and download more ebooks at ebookbell.com
  • 2.
    Here are somerecommended products that we believe you will be interested in. You can click the link to download. Practical Sql A Beginners Guide To Storytelling With Data 1st Edition Anthony Debarros https://ebookbell.com/product/practical-sql-a-beginners-guide-to- storytelling-with-data-1st-edition-anthony-debarros-22034964 Spatial Sql A Practical Approach To Modern Gis Using Sql Matthew Forrest https://ebookbell.com/product/spatial-sql-a-practical-approach-to- modern-gis-using-sql-matthew-forrest-55679710 Mastering Sql Joins A Quick Handbook On Mastering Sql Joins With Practical Exercises Wade https://ebookbell.com/product/mastering-sql-joins-a-quick-handbook-on- mastering-sql-joins-with-practical-exercises-wade-52368734 Pro Sql Server 2022 Wait Statistics A Practical Guide To Analyzing Performance In Sql Server And Azure Sql Database 3rd Edition 3rd Edition Thomas Larock https://ebookbell.com/product/pro-sql-server-2022-wait-statistics-a- practical-guide-to-analyzing-performance-in-sql-server-and-azure-sql- database-3rd-edition-3rd-edition-thomas-larock-47559882
  • 3.
    Pro Sql Server2019 Wait Statistics A Practical Guide To Analyzing Performance In Sql Server Second Edition 2nd Edition Enrico Van De Laar https://ebookbell.com/product/pro-sql-server-2019-wait-statistics-a- practical-guide-to-analyzing-performance-in-sql-server-second- edition-2nd-edition-enrico-van-de-laar-52952162 Pro Sql Server 2022 Wait Statistics A Practical Guide To Analyzing Performance In Sql Server And Azure Sql Database 3rd Edition 3rd Edition Thomas Larock https://ebookbell.com/product/pro-sql-server-2022-wait-statistics-a- practical-guide-to-analyzing-performance-in-sql-server-and-azure-sql- database-3rd-edition-3rd-edition-thomas-larock-47559896 Jooq Masterclass A Practical Guide For Java Developers To Write Sql Queries For Complex Database Interactions Anghel Leonard https://ebookbell.com/product/jooq-masterclass-a-practical-guide-for- java-developers-to-write-sql-queries-for-complex-database- interactions-anghel-leonard-56223438 Jooq Masterclass A Practical Guide For Java Developers To Write Sql Queries For Complex Database Interactions 1st Edition Anghel Leonard https://ebookbell.com/product/jooq-masterclass-a-practical-guide-for- java-developers-to-write-sql-queries-for-complex-database- interactions-1st-edition-anghel-leonard-44785934 Sql Query Design Patterns And Best Practices A Practical Guide To Writing Readable And Maintainable Sql Queries Using Its Design Patterns 1st Edition Steve Hughes https://ebookbell.com/product/sql-query-design-patterns-and-best- practices-a-practical-guide-to-writing-readable-and-maintainable-sql- queries-using-its-design-patterns-1st-edition-steve-hughes-49190592
  • 6.
    CONTENTS IN DETAIL TITLEPAGE COPYRIGHT ABOUT THE AUTHOR PREFACE TO THE SECOND EDITION ACKNOWLEDGMENTS INTRODUCTION What Is SQL? Why SQL? Who Is This Book For? What You’ll Learn CHAPTER 1: SETTING UP YOUR CODING ENVIRONMENT Installing a Text Editor Downloading Code and Data from GitHub Installing PostgreSQL and pgAdmin Windows Installation macOS Installation Linux Installation Working with pgAdmin Launching pgAdmin and Setting a Master Password Connecting to the Default postgres Database
  • 7.
    Exploring the QueryTool Customizing pgAdmin Alternatives to pgAdmin Wrapping Up CHAPTER 2: CREATING YOUR FIRST DATABASE AND TABLE Understanding Tables Creating a Database Executing SQL in pgAdmin Connecting to the analysis Database Creating a Table Using the CREATE TABLE Statement Making the teachers Table Inserting Rows into a Table Using the INSERT Statement Viewing the Data Getting Help When Code Goes Bad Formatting SQL for Readability Wrapping Up CHAPTER 3: BEGINNING DATA EXPLORATION WITH SELECT Basic SELECT Syntax Querying a Subset of Columns Sorting Data with ORDER BY Using DISTINCT to Find Unique Values Filtering Rows with WHERE Using LIKE and ILIKE with WHERE Combining Operators with AND and OR Putting It All Together
  • 8.
    Wrapping Up CHAPTER 4:UNDERSTANDING DATA TYPES Understanding Characters Understanding Numbers Using Integers Auto-Incrementing Integers Using Decimal Numbers Choosing Your Number Data Type Understanding Dates and Times Using the interval Data Type in Calculations Understanding JSON and JSONB Using Miscellaneous Types Transforming Values from One Type to Another with CAST Using CAST Shortcut Notation Wrapping Up CHAPTER 5: IMPORTING AND EXPORTING DATA Working with Delimited Text Files Handling Header Rows Quoting Columns That Contain Delimiters Using COPY to Import Data Importing Census Data Describing Counties Creating the us_counties_pop_est_2019 Table Understanding Census Columns and Data Types Performing the Census Import with COPY Inspecting the Import Importing a Subset of Columns with COPY Importing a Subset of Rows with COPY
  • 9.
    Adding a Valueto a Column During Import Using COPY to Export Data Exporting All Data Exporting Particular Columns Exporting Query Results Importing and Exporting Through pgAdmin Wrapping Up CHAPTER 6: BASIC MATH AND STATS WITH SQL Understanding Math Operators and Functions Understanding Math and Data Types Adding, Subtracting, and Multiplying Performing Division and Modulo Using Exponents, Roots, and Factorials Minding the Order of Operations Doing Math Across Census Table Columns Adding and Subtracting Columns Finding Percentages of the Whole Tracking Percent Change Using Aggregate Functions for Averages and Sums Finding the Median Finding the Median with Percentile Functions Finding Median and Percentiles with Census Data Finding Other Quantiles with Percentile Functions Finding the Mode Wrapping Up CHAPTER 7: JOINING TABLES IN A RELATIONAL DATABASE Linking Tables Using JOIN
  • 10.
    Relating Tables withKey Columns Querying Multiple Tables Using JOIN Understanding JOIN Types JOIN LEFT JOIN and RIGHT JOIN FULL OUTER JOIN CROSS JOIN Using NULL to Find Rows with Missing Values Understanding the Three Types of Table Relationships One-to-One Relationship One-to-Many Relationship Many-to-Many Relationship Selecting Specific Columns in a Join Simplifying JOIN Syntax with Table Aliases Joining Multiple Tables Combining Query Results with Set Operators UNION and UNION ALL INTERSECT and EXCEPT Performing Math on Joined Table Columns Wrapping Up CHAPTER 8: TABLE DESIGN THAT WORKS FOR YOU Following Naming Conventions Quoting Identifiers Enables Mixed Case Pitfalls with Quoting Identifiers Guidelines for Naming Identifiers Controlling Column Values with Constraints Primary Keys: Natural vs. Surrogate
  • 11.
    Foreign Keys How toAutomatically Delete Related Records with CASCADE The CHECK Constraint The UNIQUE Constraint The NOT NULL Constraint How to Remove Constraints or Add Them Later Speeding Up Queries with Indexes B-Tree: PostgreSQL’s Default Index Considerations When Using Indexes Wrapping Up CHAPTER 9: EXTRACTING INFORMATION BY GROUPING AND SUMMARIZING Creating the Library Survey Tables Creating the 2018 Library Data Table Creating the 2017 and 2016 Library Data Tables Exploring the Library Data Using Aggregate Functions Counting Rows and Values Using count() Finding Maximum and Minimum Values Using max() and min() Aggregating Data Using GROUP BY Wrapping Up CHAPTER 10: INSPECTING AND MODIFYING DATA Importing Data on Meat, Poultry, and Egg Producers Interviewing the Dataset Checking for Missing Values Checking for Inconsistent Data Values Checking for Malformed Values Using length() Modifying Tables, Columns, and Data
  • 12.
    Modifying Tables withALTER TABLE Modifying Values with UPDATE Viewing Modified Data with RETURNING Creating Backup Tables Restoring Missing Column Values Updating Values for Consistency Repairing ZIP Codes Using Concatenation Updating Values Across Tables Deleting Unneeded Data Deleting Rows from a Table Deleting a Column from a Table Deleting a Table from a Database Using Transactions to Save or Revert Changes Improving Performance When Updating Large Tables Wrapping Up CHAPTER 11: STATISTICAL FUNCTIONS IN SQL Creating a Census Stats Table Measuring Correlation with corr(Y, X) Checking Additional Correlations Predicting Values with Regression Analysis Finding the Effect of an Independent Variable with r-Squared Finding Variance and Standard Deviation Creating Rankings with SQL Ranking with rank() and dense_rank() Ranking Within Subgroups with PARTITION BY Calculating Rates for Meaningful Comparisons Finding Rates of Tourism-Related Businesses
  • 13.
    Smoothing Uneven Data WrappingUp CHAPTER 12: WORKING WITH DATES AND TIMES Understanding Data Types and Functions for Dates and Times Manipulating Dates and Times Extracting the Components of a timestamp Value Creating Datetime Values from timestamp Components Retrieving the Current Date and Time Working with Time Zones Finding Your Time Zone Setting Setting the Time Zone Performing Calculations with Dates and Times Finding Patterns in New York City Taxi Data Finding Patterns in Amtrak Data Wrapping Up CHAPTER 13: ADVANCED QUERY TECHNIQUES Using Subqueries Filtering with Subqueries in a WHERE Clause Creating Derived Tables with Subqueries Joining Derived Tables Generating Columns with Subqueries Understanding Subquery Expressions Using Subqueries with LATERAL Using Common Table Expressions Performing Cross Tabulations Installing the crosstab() Function Tabulating Survey Results
  • 14.
    Tabulating City TemperatureReadings Reclassifying Values with CASE Using CASE in a Common Table Expression Wrapping Up CHAPTER 14: MINING TEXT TO FIND MEANINGFUL DATA Formatting Text Using String Functions Case Formatting Character Information Removing Characters Extracting and Replacing Characters Matching Text Patterns with Regular Expressions Regular Expression Notation Using Regular Expressions with WHERE Regular Expression Functions to Replace or Split Text Turning Text to Data with Regular Expression Functions Full-Text Search in PostgreSQL Text Search Data Types Creating a Table for Full-Text Search Searching Speech Text Ranking Query Matches by Relevance Wrapping Up CHAPTER 15: ANALYZING SPATIAL DATA WITH POSTGIS Enabling PostGIS and Creating a Spatial Database Understanding the Building Blocks of Spatial Data Understanding Two-Dimensional Geometries Well-Known Text Formats Projections and Coordinate Systems
  • 15.
    Spatial Reference SystemIdentifier Understanding PostGIS Data Types Creating Spatial Objects with PostGIS Functions Creating a Geometry Type from Well-Known Text Creating a Geography Type from Well-Known Text Using Point Functions Using LineString Functions Using Polygon Functions Analyzing Farmers’ Markets Data Creating and Filling a Geography Column Adding a Spatial Index Finding Geographies Within a Given Distance Finding the Distance Between Geographies Finding the Nearest Geographies Working with Census Shapefiles Understanding the Contents of a Shapefile Loading Shapefiles Exploring the Census 2019 Counties Shapefile Examining Demographics Within a Distance Performing Spatial Joins Exploring Roads and Waterways Data Joining the Census Roads and Water Tables Finding the Location Where Objects Intersect Wrapping Up CHAPTER 16: WORKING WITH JSON DATA Understanding JSON Structure Considering When to Use JSON with SQL
  • 16.
    Using json andjsonb Data Types Importing and Indexing JSON Data Using json and jsonb Extraction Operators Key Value Extraction Array Element Extraction Path Extraction Containment and Existence Analyzing Earthquake Data Exploring and Loading the Earthquake Data Working with Earthquake Times Finding the Largest and Most-Reported Earthquakes Converting Earthquake JSON to Spatial Data Generating and Manipulating JSON Turning Query Results into JSON Adding, Updating, and Deleting Keys and Values Using JSON Processing Functions Finding the Length of an Array Returning Array Elements as Rows Wrapping Up CHAPTER 17: SAVING TIME WITH VIEWS, FUNCTIONS, AND TRIGGERS Using Views to Simplify Queries Creating and Querying Views Creating and Refreshing a Materialized View Inserting, Updating, and Deleting Data Using a View Creating Your Own Functions and Procedures Creating the percent_change() Function
  • 17.
    Using the percent_change()Function Updating Data with a Procedure Using the Python Language in a Function Automating Database Actions with Triggers Logging Grade Updates to a Table Automatically Classifying Temperatures Wrapping Up CHAPTER 18: USING POSTGRESQL FROM THE COMMAND LINE Setting Up the Command Line for psql Windows psql Setup macOS psql Setup Linux psql Setup Working with psql Launching psql and Connecting to a Database Running SQL Queries on psql Navigating and Formatting Results Meta-Commands for Database Information Importing, Exporting, and Using Files Additional Command Line Utilities to Expedite Tasks Adding a Database with createdb Loading Shapefiles with shp2pgsql Wrapping Up CHAPTER 19: MAINTAINING YOUR DATABASE Recovering Unused Space with VACUUM Tracking Table Size Monitoring the Autovacuum Process
  • 18.
    Running VACUUM Manually ReducingTable Size with VACUUM FULL Changing Server Settings Locating and Editing postgresql.conf Reloading Settings with pg_ctl Backing Up and Restoring Your Database Using pg_dump to Export a Database or Table Restoring a Database Export with pg_restore Exploring Additional Backup and Restore Options Wrapping Up CHAPTER 20: TELLING YOUR DATA’S STORY Start with a Question Document Your Process Gather Your Data No Data? Build Your Own Database Assess the Data’s Origins Interview the Data with Queries Consult the Data’s Owner Identify Key Indicators and Trends over Time Ask Why Communicate Your Findings Wrapping Up APPENDIX: ADDITIONAL POSTGRESQL RESOURCES PostgreSQL Development Environments PostgreSQL Utilities, Tools, and Extensions PostgreSQL News and Community Documentation
  • 19.
  • 20.
    PRACTICAL SQL 2nd Edition ABeginner’s Guide to Storytelling with Data by Anthony DeBarros
  • 21.
    PRACTICAL SQL, 2NDEDITION. Copyright © 2022 by Anthony DeBarros. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in the United States of America First printing 25 24 23 22 21 1 2 3 4 5 6 7 8 9 ISBN-13: 978-1-7185-0106-5 (print) ISBN-13: 978-1-7185-0107-2 (ebook) Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Rachel Monaghan Production Editors: Jennifer Kepler and Paula Williamson Developmental Editor: Liz Chadwick Cover Illustrator: Josh Ellingson Interior Design: Octopod Studios Technical Reviewer: Stephen Frost Copyeditor: Kim Wimpsett Compositor: Maureen Forys, Happenstance Type-O-Rama Proofreader: Liz Wheeler For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; info@nostarch.com www.nostarch.com The Library of Congress has catalogued the first edition as follows: Names: DeBarros, Anthony, author. Title: Practical SQL : a beginner's guide to storytelling with data / Anthony DeBarros. Description: San Francisco : No Starch Press, 2018. | Includes index. Identifiers: LCCN 2018000030 (print) | LCCN 2017043947 (ebook) | ISBN 9781593278458 (epub) | ISBN 1593278454 (epub) | ISBN 9781593278274 (paperback) | ISBN 1593278276 (paperback) | ISBN 9781593278458 (ebook) Subjects: LCSH: SQL (Computer program language) | Database design. | BISAC: COMPUTERS / Programming Languages / SQL. | COMPUTERS / Database Management / General. | COMPUTERS / Database Management / Data Mining. Classification: LCC QA76.73.S67 (print) | LCC QA76.73.S67 D44 2018 (ebook) | DDC 005.75/6--dc23 LC record available at https://lccn.loc.gov/2018000030 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
  • 22.
    The information inthis book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
  • 23.
    About the Author AnthonyDeBarros is a longtime journalist and early adopter of “data journalism,” the use of spreadsheets, databases, and code to find news in data. He’s currently a data editor for the Wall Street Journal, where he covers topics including the economy, trade, demographics, and the Covid- 19 pandemic. Previously, he worked for the Gannett company at USA Today and the Poughkeepsie Journal and held product development and content strategy roles for Questex and DocumentCloud. About the Technical Reviewer Stephen Frost is the chief technology officer at Crunchy Data. He has been working with PostgreSQL since 2003 and general database technology since before then. Stephen began contributing to PostgreSQL development in 2004 and has been involved in the development of the role system, column-level privileges, row-level security, GSSAPI encryption, and the predefined roles system. He has also served on the board of the United States PostgreSQL Association and Software in the Public Interest, regularly speaks at PostgreSQL Community conferences and events, and works as a member of various PostgreSQL community teams.
  • 24.
    PREFACE TO THESECOND EDITION Since the publication of the first edition of Practical SQL, I’ve received kind notes about the book from readers around the world. One happy reader said it helped him ace SQL questions on a job interview. Another, a teacher, wrote to say that his students remarked favorably about having the book assigned for class. Others just wanted to say thanks because they found the book helpful and a good read, two pieces of feedback that will warm the heart of most any author. I also sometimes heard from readers who hit a roadblock while working through an exercise or who had trouble with software or data files. I paid close attention to those emails, especially when the same question seemed to crop up more than once. Meanwhile, during my own journey of learning SQL—I use it every day at work—I’d often discover a technique and wish that I’d included it in the book. With all that in mind, I approached the team at No Starch Press with the idea of updating and expanding Practical SQL into a second edition. I’m thankful they said yes. This new version of the book is more complete, offers stronger guidance for readers related to software and code, and clarifies information that wasn’t as clear or presented as accurately as it could have been. The book has been thoroughly enjoyable to revisit, and I’ve learned much along the way.
  • 25.
    This second editionincludes numerous updates, expansions, and clarifications in every chapter. Throughout, I’ve been careful to note when code syntax adheres to the SQL standard—meaning you can generally use it across database systems—or when the syntax is specific to the database used in the book, PostgreSQL. The following are among the most substantial changes: Two chapters are new. Chapter 1, “Setting Up Your Coding Environment,” details how to install PostgreSQL, pgAdmin, and additional PostgreSQL components on multiple operating systems. It also shows how to obtain the code listings and data from GitHub. In the first edition, this information was located in the introduction and occasionally missed by readers. Chapter 16, “Working with JSON Data,” covers PostgreSQL’s support for the JavaScript Object Notation data format, using datasets about movies and earthquakes. In Chapter 4 on data types, I’ve added a section on IDENTITY, the ANSI SQL standard implementation for auto-incrementing integer columns. Throughout the book, IDENTITY replaces the PostgreSQL-specific serial auto-incrementing integer type so that code examples more closely reflect the SQL standard. Chapter 5 on importing and exporting data now includes a section about using the WHERE keyword with the COPY command to filter which rows are imported from a source file to a table. I’ve removed the user-created median() function from Chapter 6 on basic math in favor of focusing exclusively on the SQL standard percentile_cont() function for calculating medians. In Chapter 7 on table joins, I’ve added a section covering the set operators UNION, UNION ALL, INTERSECT, and EXCEPT. Additionally, I’ve added a section covering the USING clause in joins to reduce redundant output and simplify query syntax. Chapter 10 on inspecting and modifying data includes a new section on using the RETURNING keyword in an UPDATE statement to display the data that the statement modified. I’ve also added a section that describes how to use the TRUNCATE command to remove all rows from a table and restart an IDENTITY sequence.
  • 26.
    In Chapter 11on statistical functions, a new section demonstrates how to create a rolling average to smooth uneven data to get a better sense of trends over time. I’ve also added information on functions for calculating standard deviation and variance. Chapter 13 on advanced query techniques now shows how to use the LATERAL keyword with subqueries. One benefit is that, by combining LATERAL with JOIN, you get functionality similar to a for loop in a programming language. In Chapter 15 on analyzing spatial data, I demonstrate how to use the Geometry Viewer in pgAdmin to see geographies placed on a map. This feature was added to pgAdmin after publication of the first edition. In Chapter 17 on views, functions, and triggers, I’ve added information about materialized views and showed how their behavior differs from standard views. I also cover procedures, which PostgreSQL now supports in addition to functions. Finally, where practical, datasets have been updated to the most recent available at the time of writing. This primarily applies to US Census population statistics but also includes the text of presidential speeches and library usage statistics. Thank you for reading Practical SQL! If you have any questions or feedback, please get in touch by emailing practicalsqlbook@gmail.com.
  • 27.
    ACKNOWLEDGMENTS This second editionof Practical SQL is the work of many hands. My thanks, first, go to the team at No Starch Press. Thanks to Bill Pollock for capturing the vision and sharpening the initial concept for the book—and for agreeing to let me have another go at it. Special thanks and appreciation to senior editor Liz Chadwick, who improved each chapter with her insightful suggestions and deft editing, and to copyeditor Kim Wimpsett and the production team of Paula Williamson and Jennifer Kepler. Stephen Frost, chief technology officer at Crunchy Data and longtime contributor to the PostgreSQL community, served as the technical reviewer for this edition. I deeply appreciate the time Stephen took to explain the inner workings of PostgreSQL and SQL concepts. This book is better, more thorough, and more accurate thanks to his detailed eye. I’d also like to acknowledge Josh Berkus, whose many contributions as technical reviewer for the first edition persist in this new version. Thank you to Investigative Reporters and Editors (IRE) and its members and staff past and present for training journalists to find stories in data. IRE is where I got my start with SQL and data journalism. Many of my colleagues have not only imparted memorable lessons on data analysis, they’ve also made my workdays brighter. Special thanks to Paul Overberg for sharing his vast knowledge of demographics and the US Census, to Lou Schilling for many technical lessons, to Christopher Schnaars for his SQL expertise, to Peter Matseykanets for his encouragement, and to Chad Day, John West, and Maureen Linke and the WSJ DC visuals team for continual inspiration. My deepest appreciation goes to my dear wife, Elizabeth, and our sons. You are the brightest lights in my day. As we are fond of saying, “To the journey . . .”
  • 28.
    INTRODUCTION Shortly after joiningthe staff of USA Today, I received a dataset that I would analyze almost every week for the next decade. It was the weekly Best-Selling Books list, which ranked the nation’s top-selling titles based on confidential sales data. Not only did the list produce an endless stream of story ideas to pitch, it also captured the zeitgeist of America in a singular way. Did you know that cookbooks sell a bit more during the week of Mother’s Day or that Oprah Winfrey turned many obscure writers into number-one best-selling authors just by having them on her show? Every week, the book list editor and I pored over the sales figures and book genres, ranking the data in search of a new headline. Rarely did we come up empty: we chronicled everything from the rocket-rise of the blockbuster Harry Potter series to the fact that Oh, the Places You’ll Go! by Dr. Seuss had become a perennial gift for new graduates. My technical companion in that time was the database programming language SQL (for Structured Query Language). Early on, I convinced USA Today’s IT department to grant me access to the SQL-based database system that powered our book list application. Using SQL, I was able to discover the stories hidden in the database, which contained sales data related to titles, authors, genres, and the codes that defined the publishing world.
  • 29.
    SQL has beenuseful to me ever since, whether my role was in product development, in content strategy, or, lately, as a data editor for the Wall Street Journal. In each case, SQL has helped me find interesting stories in data—and that’s exactly what you’ll learn to do using this book. What Is SQL? SQL is a widely used programming language for managing data and database systems. Whether you’re a marketing analyst, a journalist, or a researcher mapping neurons in the brain of a fruit fly, you’ll benefit from using SQL to collect, modify, explore, and summarize data. Because SQL is a mature language that’s been around for decades, it’s ingrained in many modern systems. A pair of IBM researchers first outlined the syntax for SQL (then called SEQUEL) in a 1974 paper, building on the theoretical work of the British computer scientist Edgar F. Codd. In 1979, a precursor to the database company Oracle (then called Relational Software) became the first to use the language in a commercial product. Today, SQL still ranks as one of the most-used computer languages in the world, and that’s unlikely to change soon. Each database system, such as PostgreSQL, MySQL or Microsoft SQL Server, implements its own variant of SQL, so you’ll notice subtle—or sometimes significant—differences in syntax if you jump from one system to another. There are several reasons behind this. The American National Standards Institute (ANSI) adopted a standard for SQL in 1986, followed by the International Organization for Standardization (ISO) in 1987. But the standard doesn’t cover all aspects of SQL that are required for a database implementation—for example, it has no entry for creating indexes. That leaves each database system maker to choose how to implement features the standard doesn’t cover—and no database maker currently claims to conform to the entire standard. Meanwhile, business considerations can lead commercial database vendors to create nonstandard SQL features for both competitive advantage and as a way to keep users in their ecosystem. For example, Microsoft’s SQL Server uses the proprietary Transact-SQL (T-SQL) that includes a number of features not in the SQL standard, such as its syntax for declaring
  • 30.
    local variables. Migratingcode written using T-SQL to another database system may not be trivial, therefore. In this book, the examples and code use the PostgreSQL database system. PostgreSQL, or simply Postgres, is a robust application that can handle large amounts of data. Here are some reasons PostgreSQL is a great choice to use with this book: It’s free. It’s available for Windows, macOS, and Linux operating systems. Its SQL implementation aims to closely follow the SQL standard. It’s widely used, so finding help online is easy. Its geospatial extension, PostGIS, lets you analyze geometric data and perform mapping functions and is often used with mapping software such as QGIS. It’s available in cloud computing environments such as Amazon Web Services and Google Cloud. It’s a common choice as a data store for web applications, including those powered by the popular web framework Django. The good news is that the fundamental concepts and much of the core SQL syntactical conventions of PostgreSQL will work across databases. So, if you’re using MySQL at work, you can employ much of what you learn here—or easily find parallel code concepts. When syntax is PostgreSQL- specific, I make sure to point that out. If you need to learn the SQL syntax of a system with features that deviate from the standard, such as Microsoft SQL Server’s T-SQL, you may want to further explore a resource focusing on that system. Why SQL? SQL certainly isn’t the only option for crunching data. Many people start with Microsoft Excel spreadsheets and their assortment of analytic functions. After working with Excel, they might graduate to Access, the database system built into some versions of Microsoft Office, which has a
  • 31.
    graphical query interfacethat makes it easy to get work done. So why learn SQL? One reason is that Excel and Access have their limits. Excel currently allows 1,048,576 rows maximum per worksheet. Access limits database size to two gigabytes and limits columns to 255 per table. It’s not uncommon for datasets to surpass those limits. The last obstacle you want to discover while facing a deadline is that your database system doesn’t have the capacity to get the job done. Using a robust SQL database system allows you to work with terabytes of data, multiple related tables, and thousands of columns. It gives you fine- grained control over the structure of your data, leading to efficiency, speed, and—most important—accuracy. SQL is also an excellent adjunct to programming languages used in the data sciences, such as R and Python. If you use either language, you can connect to SQL databases and, in some cases, even incorporate SQL syntax directly into the language. For people with no background in programming languages, SQL often serves as an easy-to-understand introduction into concepts related to data structures and programming logic. Finally, SQL is useful beyond data analysis. If you delve into building online applications, you’ll find that databases provide the backend power for many common web frameworks, interactive maps, and content management systems. When you need to dig beneath the surface of these applications, the ability to manage data and databases with SQL will come in very handy.
  • 32.
    Who Is ThisBook For? Practical SQL is for people who encounter data in their everyday lives and want to learn how to analyze, manage, and transform it. With that in mind, we cover real-world data and scenarios, such as US Census demographics, crime reports, and data about taxi rides in New York City. We aim to understand not only how SQL works but how we can use it to find valuable insights. This book was written with people new to programming in mind, so the early chapters cover key basics about databases, data, and SQL syntax. Readers with some SQL experience should benefit from later chapters that cover more advanced topics, such as Geographical Information Systems (GIS). I assume that you know your way around your computer, including how to install programs, navigate your hard drive, and download files from the internet, but I don’t assume you have any experience with programming or data analysis. What You’ll Learn Practical SQL starts with a chapter on setting up your system and getting the code and data examples and then moves through the basics of databases, queries, tables, and data that are common to SQL across many database systems. Chapters 14 to 19 cover topics more specific to PostgreSQL, such as full-text search, functions, and GIS. Although many chapters in this book can stand alone, you should work through the book sequentially to build on the fundamentals. Datasets presented in early chapters often reappear later, so following the book in order will help you stay on track. The following summary provides more detail about each chapter: Chapter 1: Setting Up Your Coding Environment walks through setting up PostgreSQL, the pgAdmin user interface, and a text editor, plus how to download example code and data. Chapter 2: Creating Your First Database and Table provides step-by- step instructions for the process of loading a simple dataset about teachers
  • 33.
    into a newdatabase. Chapter 3: Beginning Data Exploration with SELECT explores basic SQL query syntax, including how to sort and filter data. Chapter 4: Understanding Data Types explains the definitions for setting columns in a table to hold specific types of data, from text to dates to various forms of numbers. Chapter 5: Importing and Exporting Data explains how to use SQL commands to load data from external files and then export it. You’ll load a table of US Census population data that you’ll use throughout the book. Chapter 6: Basic Math and Stats with SQL covers arithmetic operations and introduces aggregate functions for finding sums, averages, and medians. Chapter 7: Joining Tables in a Relational Database explains how to query multiple, related tables by joining them on key columns. You’ll learn how and when to use different types of joins. Chapter 8: Table Design that Works for You covers how to set up tables to improve the organization and integrity of your data as well as how to speed up queries using indexes. Chapter 9: Extracting Information by Grouping and Summarizing explains how to use aggregate functions to find trends in US library usage based on annual surveys. Chapter 10: Inspecting and Modifying Data explores how to find and fix incomplete or inaccurate data using a collection of records about meat, egg, and poultry producers as an example. Chapter 11: Statistical Functions in SQL introduces correlation, regression, ranking, and other functions to help you derive more meaning from datasets. Chapter 12: Working with Dates and Times explains how to create, manipulate, and query dates and times in your database, including working with time zones and with data about New York City taxi trips and Amtrak train schedules.
  • 34.
    Chapter 13: AdvancedQuery Techniques explains how to use more complex SQL operations such as subqueries and cross tabulations, plus the CASE statement, to reclassify values in a dataset on temperature readings. Chapter 14: Mining Text to Find Meaningful Data covers how to use PostgreSQL’s full-text search engine and regular expressions to extract data from unstructured text, using police reports and a collection of speeches by US presidents as examples. Chapter 15: Analyzing Spatial Data with PostGIS introduces data types and queries related to spatial objects, which will let you analyze geographical features such as counties, roads, and rivers. Chapter 16: Working with JSON Data introduces the JavaScript Object Notation (JSON) data format and uses data about movies and earthquakes to explore PostgreSQL JSON support. Chapter 17: Saving Time with Views, Functions, and Triggers explains how to automate database tasks so you can avoid repeating routine work. Chapter 18: Using PostgreSQL from the Command Line covers how to use text commands at your computer’s command prompt to connect to your database and run queries. Chapter 19: Maintaining Your Database provides tips and procedures for tracking the size of your database, customizing settings, and backing up data. Chapter 20: Telling Your Data’s Story provides guidelines for generating ideas for analysis, vetting data, drawing sound conclusions, and presenting your findings clearly. Appendix: Additional PostgreSQL Resources lists software and documentation to help you grow your skills. Each chapter ends with a “Try It Yourself” section that contains exercises to help you reinforce the topics you learned. Ready? Let’s begin with Chapter 1, “Setting Up Your Coding Environment.”
  • 35.
    1 SETTING UP YOURCODING ENVIRONMENT Let’s begin by installing the resources you’ll need to complete the exercises in the book. In this chapter, you’ll install a text editor, download the example code and data, and then install the PostgreSQL database system and its companion graphical user interface, pgAdmin. I’ll also tell you how to get help if you need it. When you’re finished, your computer will have a robust environment for you to learn how to analyze data with SQL. Avoid the temptation to skip ahead to the next chapter. My high school teacher (clearly a fan of alliteration) used to tell us that “proper planning prevents poor performance.” If you follow all the steps in this chapter, you’ll avoid headaches later. Our first task is to set up a text editor suitable for working with data. Installing a Text Editor
  • 36.
    The source datayou’ll add to a SQL database is typically stored in multiple text files, often in a format called comma-separated values (CSV). You’ll learn more about the CSV format in Chapter 5, in the section “Working with Delimited Text Files,” but for now let’s make sure you have a text editor that will let you open those files without inadvertently harming the data. Common business applications—word processors and spreadsheet programs—tend to introduce styles or hidden characters into files without asking, and that makes using them for data work problematic, as data software expects data in precise formats. For example, if you open a CSV file with Microsoft Excel, the program will automatically alter some data to make it more human-readable; it will assume, for example, that an item code of 3-09 is a date and format it as 9-Mar. Text editors deal exclusively with plain text with no embellishments such as formatting, and for that reason programmers use them to edit files that hold source code, data, and software configurations—all cases where you want your text to be treated as text, and nothing more. Any text editor should work for the book’s purposes, so if you have a favorite, feel free to use it. Here are some I have used and recommend. Except where noted, they are free and available for macOS, Windows, and Linux. Visual Studio Code by Microsoft: https://code.visualstudio.com/ Atom by GitHub: https://atom.io/ Sublime Text by Sublime HQ (free to evaluate but requires purchase for continued use): https://www.sublimetext.com/ Notepad++ by author Don Ho (Windows only): https://notepad-plus- plus.org/ (note that this is a different application than Notepad.exe, which comes with Windows) More advanced users who prefer to work in the command line may want to use one of these two text editors, which are installed by default in macOS and Linux: vim by author Bram Moolenaar and the open source community: https://www.vim.org/
  • 37.
    GNU nano byauthor Chris Allegretta and the open source community: https://www.nano-editor.org/ If you don’t have a text editor, download and install one and get familiar with the basics of opening folders and working with files. Next, let’s get the book’s example code and data. Downloading Code and Data from GitHub All of the code and data you’ll need for working through the book’s exercises are available for download. To get it, follow these steps: . Visit the book’s page on the No Starch Press website at https://nostarch.com/practical-sql-2nd-edition/. . On the page, click Download the code from GitHub to visit the repository on https://github.com/ that holds the material. . On the Practical SQL 2nd Edition page at GitHub, you should see a Code button. Click it, and then select Download ZIP to save the ZIP file to your computer. Place it in a location where you can easily find it, such as your desktop. (If you’re a GitHub user, you can also clone or fork the repository.) . Unzip the file. You should then see a folder named practical-sql-2-master that contains the various files and subfolders for the book. Again, place this folder where you can easily find it. NOTE Windows users will need to provide permission for the PostgreSQL database you will install to read and write to the contents of the practical-sql-2-master folder. To do so, right-click the folder, click Properties, and click the Security tab. Click Edit and then Add. Type the name Everyone into the object names box and click OK. Highlight Everyone in the user list, select all boxes under Allow, and then click Apply and OK.
  • 38.
    Inside the practical-sql-2-masterfolder, for each chapter you’ll find a subfolder named Chapter_XX (XX is the chapter number). Inside subfolders, each chapter that includes code examples will also have a file named Chapter_XX that ends with a .sql extension. This is a SQL code file that you can open with your text editor or with the PostgreSQL administrative tool you’ll install later in this chapter. Note that in the book several code examples are truncated to save space, but you’ll need the full listing from the .sql file to complete the exercise. You’ll know an example is truncated when you see --snip-- in the listing. The chapter folders also contain the public data you’ll use in the exercises, stored in CSV and other text-based files. As noted, it’s fine to view CSV files with a true text editor, but don’t open these files with Excel or a word processor. Now, with the prerequisites complete, let’s load the database software. Installing PostgreSQL and pgAdmin In this section, you’ll install both the PostgreSQL database system and a companion graphical administrative tool, pgAdmin. Think of pgAdmin as a helpful visual workspace for managing your PostgreSQL database. Its interface lets you see your database objects, manage settings, import and export data, and write queries, which is the code that retrieves data from your database. One benefit of using PostgreSQL is that the open source community has provided excellent guidelines that make it easy to get PostgreSQL up and running. The following sections outline installation for Windows, macOS, and Linux as of this writing, but the steps might change as new versions of the software or operating systems are released. Check the documentation noted in each section as well as the GitHub repository with the book’s resources; I’ll maintain files there with updates and answers to frequently asked questions.
  • 39.
    NOTE I recommend youinstall the latest available version of PostgreSQL for your operating system to ensure that it’s up-to-date on security patches and new features. For this book, I’ll assume you’re using version 11.0 or later. Windows Installation For Windows, I recommend using the installer provided by the company EDB (formerly EnterpriseDB), which offers support and services for PostgreSQL users. When you download the PostgreSQL package bundle from EDB, you also get pgAdmin and Stack Builder, which includes a few other tools you’ll use in this book and throughout your SQL career. To get the software, visit https://www.postgresql.org/download/windows/ and click Download the installer in the EDB section. This should lead to a downloads page on the EDB site. Select the latest available 64-bit Windows version of PostgreSQL unless you’re using an older PC with 32-bit Windows. NOTE The following section covers installation for Windows 10. If you’re using Windows 11, please check the GitHub repository with the book’s resources for notes about any adjustments to these instructions. After you download the installer, follow these steps to install PostgreSQL, pgAdmin, and additional components: . Right-click the installer and select Run as administrator. Answer Yes to the question about allowing the program to make changes to your computer. The program will perform a setup task and then present an initial welcome screen. Click through it. . Choose your installation directory, accepting the default.
  • 40.
    . On theSelect Components screen, select the boxes to install PostgreSQL Server, the pgAdmin tool, Stack Builder, and the command line tools. . Choose the location to store data. You can choose the default, which is in a data subdirectory in the PostgreSQL directory. . Choose a password. PostgreSQL is robust with security and permissions. This password is for the default initial database superuser account, which is called postgres. . Select the default port number where the server will listen. Unless you have another database or application using it, use the default, which should be 5432. You can substitute 5433 or another number if you already have an application using the default port. . Select your locale. Using the default is fine. Then click through the summary screen to begin the installation, which will take several minutes. . When the installation is done, you’ll be asked whether you want to launch EnterpriseDB’s Stack Builder to obtain additional packages. Make sure the box is checked and click Finish. . When Stack Builder launches, choose the PostgreSQL installation on the drop-down menu and click Next. A list of additional applications should download. . Expand the Spatial Extensions menu and select the PostGIS Bundle for the version of PostgreSQL you installed. You may see more than one listed; if so, choose the newest version. Also, expand the Add-ons, tools and utilities menu and select EDB Language Pack, which installs support for programming languages including Python. Click through several times; you’ll need to wait while the installer downloads the additional components. When installation files have been downloaded, click Next to install the language and PostGIS components. For PostGIS, you’ll need to agree to the license terms; click through until you’re asked to Choose Components. Make sure PostGIS and Create spatial database are selected. Click Next, accept the default install location, and click Next again. . Enter your database password when prompted and continue through the prompts to install PostGIS.
  • 41.
    . Answer Yeswhen asked to register the PROJ_LIB and GDAL_DATA environment variables. Also, answer Yes to the questions about setting POSTGIS_ENABLED_DRIVERS and enabling the POSTGIS_ENABLE_OUTDB_RASTERS environment variable. Finally, click through the final Finish steps to complete the installation and exit the installers. Depending on the version, you may be prompted to restart your computer. When finished, you should have two new folders in your Windows Start menu: one for PostgreSQL and another for PostGIS. If you’d like to get started right away, you can skip ahead to the section “Working with pgAdmin.” Otherwise, follow the steps in the next section to set environment variables for optional Python language support. We cover using Python with PostgreSQL in Chapter 17; you can wait until then to set up Python if you’d like to move ahead now. Configuring Python Language Support In Chapter 17, you’ll learn how to use the Python programming language with PostgreSQL. In the previous section, you installed the EDB Language Pack, which provides Python support. Follow these steps to add the location of the Language Pack files to your Windows system’s environment variables: . Open the Windows Control Panel by clicking the Search icon on the Windows taskbar, entering Control Panel, and then clicking the Control Panel icon. . In the Control Panel app, enter Environment in the search box. In the list of search results displayed, click Edit the System Environment Variables. A System Properties dialog will appear. . In the System Properties dialog, on the Advanced tab, click Environment Variables. The dialog that opens has two sections: User variables and System variables. In the System variables section, if you don’t see a PATH variable, continue to step a to create a new one. If you do see an existing PATH variable, continue to step b to modify it.
  • 42.
    If you don’tsee PATH in the System variables section, click New to open a New System Variable dialog, shown in Figure 1-1. Figure 1-1: Creating a new PATH environment variable in Windows 10 In the Variable name box, enter PATH. In the Variable value box, enter C:edblanguagepackv2Python-3.9. (Instead of typing, you can click Browse Directory and navigate to the directory in the Browse For Folder dialog.) When you’ve either entered the path manually or browsed to it, click OK on the dialog to close it. . If you do see an existing PATH variable in the System variables section, highlight it and click Edit. In the list of variables that displays, click New and enter C:edblanguagepackv2Python-3.9. (Instead of typing, you can click Browse Directory and navigate to the directory in the Browse For Folder dialog.) Once you’ve added the Language Pack path, highlight it in the list of variables and click Move Up until the path is at the top of the variables list. That way, PostgreSQL will find the correct Python version if you have additional Python installations. The result should look like the highlighted line in Figure 1-2. Click OK to close the dialog.
  • 43.
    Figure 1-2: Editingexisting PATH environment variables in Windows 10 . Finally, in the System variables section, click New. In the New System Variable dialog, enter PYTHONHOME in the Variable name box. In the Variable value box, enter C:edblanguagepackv2Python-3.9. When you’re finished, click OK in all dialogs to close them. Note that these Python path settings will take effect the next time you restart your system.
  • 44.
    If you experienceany hiccups during the PostgreSQL install, check the resources for the book, where I will note changes that occur as the software is developed and can also answer questions. If you’re unable to install PostGIS via Stack Builder, try downloading a separate installer from the PostGIS site at https://postgis.net/windows_downloads/ and consult the guides at https://postgis.net/documentation/. Now, you can move ahead to the section “Working with pgAdmin.” macOS Installation For macOS users, I recommend obtaining Postgres.app, an open source macOS application that includes PostgreSQL as well as the PostGIS extension and a few other goodies. Separately, you’ll need to install the pgAdmin GUI and the Python language for use in functions. Installing Postgres.app and pgAdmin Follow these steps: . Visit https://postgresapp.com/ and download the latest release of the app. This will be a Disk Image file that ends in .dmg. . Double-click the .dmg file to open it, and then drag and drop the app icon into your Applications folder. . In your Applications folder, double-click the app icon to launch Postgres.app. (If you see a dialog that says the app cannot be opened because the developer cannot be verified, click Cancel. Then right-click the app icon and choose Open.) When Postgres.app opens, click Initialize to create and start a PostgreSQL database server. A small elephant icon will appear in your menu bar to indicate that you now have a database running. To set up the included PostgreSQL command line tools so you’re able to use them in future, open your Terminal application and run the following single line of code at the prompt (you can copy the code as a single line from the Postgres.app site at https://postgresapp.com/documentation/install.html):
  • 45.
    sudo mkdir -p/etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp You may be prompted for the password you use to log in to your Mac. Enter that. The commands should execute without providing any output. Next, because Postgres.app doesn’t include pgAdmin, follow these steps to install pgAdmin: . Visit the pgAdmin site’s page for macOS downloads at https://www.pgadmin.org/download/pgadmin-4-macos/. . Select the latest version and download the installer (look for a Disk Image file that ends in .dmg). . Double-click the .dmg file, click through the prompt to accept the terms, and then drag pgAdmin’s elephant app icon into your Applications folder. Installation on macOS is relatively simple, but if you encounter any issues, review the documentation for Postgres.app at https://postgresapp.com/documentation/ and for pgAdmin at https://www.pgadmin.org/docs/. Installing Python In Chapter 17, you’ll learn how to use the Python programming language with PostgreSQL. To use Python with Postgres.app, you must install a specific version of the language even though macOS comes with Python pre-installed (and you might have set up an additional Python environment). To enable Postgres.app’s optional Python language support, follow these steps: . Visit the official Python site at https://www.python.org/ and click the Downloads menu. . In the list of releases, find and download the latest version of Python 3.9. Choose the appropriate installer for your Mac’s processor—an Intel chip on older Macs or Apple Silicon for newer models. The download is an Apple software package file that ends in .pkg.
  • 46.
    . Double-click thepackage file to install Python, clicking through license agreements. Close the installer when finished. Python requirements for Postgres.app may change over time. Check its Python documentation at https://postgresapp.com/documentation/plpython.html as well as the resources for this book for updates. You’re now ready to move ahead to the section “Working with pgAdmin.” Linux Installation If you’re a Linux user, installing PostgreSQL becomes simultaneously easy and difficult, which in my experience is very much the way it is in the Linux universe. Most times you can accomplish an installation with a few commands, but finding those commands requires some Internet sleuth work. Thankfully, most popular Linux distributions—including Ubuntu, Debian, and CentOS—bundle PostgreSQL in their standard package. However, some distributions stay on top of updates more than others, so there’s a chance the PostgreSQL you have downloaded may not be the latest. The best path is to consult your distribution’s documentation for the best way to install PostgreSQL if it’s not already included or if you want to upgrade to a more recent version. Alternatively, the PostgreSQL project maintains complete up-to-date package repositories for Red Hat variants, Debian, and Ubuntu. Visit https://yum.postgresql.org/ and https://wiki.postgresql.org/wiki/Apt for details. The packages you’ll want to install include the client and server for PostgreSQL, pgAdmin (if available), PostGIS, and PL/Python. The exact names of these packages will vary according to your Linux distribution. You might also need to manually start the PostgreSQL database server. The pgAdmin app is rarely part of Linux distributions. To install it, refer to the pgAdmin site at https://www.pgadmin.org/download/ for the latest instructions and to see whether your platform is supported. If you’re feeling adventurous, you can find instructions on building the app from source code at https://www.pgadmin.org/download/pgadmin-4-source-code/. Once finished, you can move ahead to the section “Working with pgAdmin.”
  • 47.
    Ubuntu Installation Example Togive you a sense of what a PostgreSQL Linux install looks like, here are the steps I took to load PostgreSQL, pgAdmin, PostGIS, and PL/Python on Ubuntu 21.04, codenamed Hirsute Hippo. It’s a combination of the directions found at https://wiki.postgresql.org/wiki/Apt plus the “Basic Server Setup” section at https://help.ubuntu.com/community/PostgreSQL/. You can follow along if you’re on Ubuntu. Open your Terminal by pressing CTRL-ALT-T. Then, at the prompt, enter the following lines to import a key for the PostgreSQL APT repository: sudo apt-get install curl ca-certificates gnupg curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - Next, run this single line to create the file /etc/apt/sources.list.d/pgdg.list: sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)- pgdg main" > /etc/apt/sources.list.d/pgdg.list' Once that’s done, update the package lists and install PostgreSQL and pgAdmin with the next two lines. Here, I installed PostgreSQL 13; you can choose a newer version if available. sudo apt-get update sudo apt-get install postgresql-13 You should now have PostgreSQL running. At the Terminal, enter the next line, which allows you to log in to the server and connect as the default postgres user to the postgres database using the psql interactive terminal, which we’ll cover in depth in Chapter 18: sudo -u postgres psql postgres When psql launches, it displays version information as well as a postgres=# prompt. Enter the following at the prompt to set a password:
  • 48.
    postgres=# password postgres Ialso like to create a user account with a name that matches my Ubuntu username. To do this, at the postgres=# prompt, enter the following line, substituting your Ubuntu username where you see anthony: postgres=# CREATE USER anthony SUPERUSER; Exit psql by entering q at the prompt. You should be back at your Terminal prompt once again. To install pgAdmin, first import a key for the repository: curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add Next, run this single line to create the file /etc/apt/sources.list.d/pgadmin4.list and update package lists: sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_rel ease -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update' Then you can install pgAdmin 4: sudo apt-get install pgadmin4-desktop Finally, to install the PostGIS and PL/Python extensions, run the following lines in your terminal (substituting the version numbers of your PostgreSQL version): sudo apt install postgresql-13-postgis-3 sudo apt install postgresql-plpython3-13 Check the official Ubuntu and PostgreSQL documentation for updates. If you experience any errors, typically with Linux an online search will yield helpful tips.
  • 49.
    Working with pgAdmin Thefinal piece of your setup puzzle is to get familiar with pgAdmin, an administration and management tool for PostgreSQL. The pgAdmin software is free, but don’t underestimate its performance; it’s a full-featured tool as powerful as paid tools such as Microsoft’s SQL Server Management Studio. With pgAdmin, you get a graphical interface where you can configure multiple aspects of your PostgreSQL server and databases, and— most appropriately for this book—use a SQL query tool for writing, running, and saving queries. Launching pgAdmin and Setting a Master Password Assuming you followed the installation steps for your operating system earlier in the chapter, here’s how to launch pgAdmin: Windows: Go to the Start menu, find the PostgreSQL folder for the version you installed, click it, and then select pgAdmin4. macOS: Click the pgAdmin icon in your Applications folder, making sure you’ve also launched Postgres.app. Linux: Startup may differ depending on your Linux distribution. Typically, at your Terminal prompt, enter pgadmin4 and press ENTER. In Ubuntu, pgAdmin appears as an app in the Activities Overview. You should see the pgAdmin splash screen, followed by the application opening, as in Figure 1-3. If it’s your first time launching pgAdmin, you’ll also receive a prompt to set a master password. This password is not related to the one you set up during the PostgreSQL install. Set a master password and click OK.
  • 50.
    NOTE On macOS, whenyou launch pgAdmin the first time, a dialog might appear that displays “pgAdmin4.app can’t be opened because it is from an unidentified developer.” Right-click the icon and click Open. The next dialog should give you the option to open the app; going forward, your Mac will remember you’ve granted this permission. Figure 1-3: The pgAdmin app running on Windows 10 The pgAdmin layout includes a left vertical pane that displays an object browser where you can view available servers, databases, users, and other objects. Across the top of the screen is a collection of menu items, and below those are tabs to display various aspects of database objects and performance. Next, let’s connect to your database.
  • 51.
    Connecting to theDefault postgres Database PostgreSQL is a database management system, which means it’s software that allows you to define, manage, and query databases. When you installed PostgreSQL, it created a database server—an instance of the application running on your computer—that includes a default database called postgres. A database is a collection of objects that includes tables, functions, and much more, and this is where your actual data will lie. We use the SQL language (as well as pgAdmin) to manage objects and data stored in the database. In the next chapter, you’ll create your own database on your PostgreSQL server to organize your work. For now, let’s connect to the default postgres database to explore pgAdmin. Use the following steps: . In the object browser, click the downward-pointing arrow to the left of the Servers node to show the default server. Depending on your operating system, the default server name could be localhost or PostgreSQL x, where x is the Postgres version number. . Double-click the server name. If prompted, enter the database password you chose during installation (you can choose to save the password so you don’t need type it in the future). A brief message appears while pgAdmin is establishing a connection. When you’re connected, several new object items should display under the server name. . Expand Databases and then expand the default database postgres. . Under postgres, expand the Schemas object, and then expand public. Your object browser pane should look similar to Figure 1-4.
  • 52.
    Figure 1-4: ThepgAdmin object browser
  • 53.
    NOTE If pgAdmin doesn’tshow a default under Servers, you’ll need to add it. Right-click Servers, and click Create▶Server. In the dialog, type a name for your server in the General tab. On the Connection tab, in the Host name/address box, enter localhost. Fill in your username and the password you supplied when installing PostgreSQL and then click Save. You should now see your server listed. This collection of objects defines every feature of your database server. That includes tables, where we store data. To view a table’s structure or perform actions on it with pgAdmin, you can access the table here. In Chapter 2, you’ll use this browser to create a new database and leave the default postgres as is. Exploring the Query Tool The pgAdmin app includes a Query Tool, which is where you write and execute code. To open the Query Tool, in pgAdmin’s object browser, first click once on any database to highlight it. For example, click the postgres database and then select Tools▶Query Tool. You’ll see three panes: a Query Editor, a Scratch Pad for holding code snippets while you work, and a Data Output pane that displays query results. You can open multiple tabs to connect to and write queries for different databases or just to organize your code the way you would like. To open another tab, click a database in the object browser and open the Query Tool again via the menu. Let’s run a simple query and see its output, using the statement in Listing 1-1 that returns the version of PostgreSQL you’ve installed. This code, along with all the examples in this book, is available for download via the resources at https://nostarch.com/practical-sql-2nd-edition/ by clicking the link Download the code from GitHub. SELECT version(); Listing 1-1: Checking your PostgreSQL version
  • 54.
    Enter the codeinto the Query Editor or, if you downloaded the book’s code from GitHub, click the Open File icon on the pgAdmin toolbar and navigate to the folder where you saved the code to open the file Chapter_01.sql in the Chapter_01 folder. To execute the statement, highlight the line beginning with SELECT and click the Execute/Refresh icon in the toolbar (it’s shaped like a play button). PostgreSQL should return the server’s version as a result in the pgAdmin Data Output pane, as in Figure 1-5 (you may need to expand the column in the Data Output pane by clicking on the right edge and dragging to your right to see the full results). You’ll learn much more about queries later in the book, but for now all you need to know is that this query uses a PostgreSQL-specific function called version() to retrieve the version information for the server. In my case, the output shows that I’m running PostgreSQL 13.3, and it provides additional specifics on the build of the software. NOTE Most of the sample code files you can download from GitHub contain more than one query. To run just one query at a time, first highlight the code for that query and then click Execute/Refresh.
  • 55.
    Random documents withunrelated content Scribd suggests to you:
  • 56.
    Life is justone breathless bustle now-a-days. Hardly had we got our minds adjusted to the engineers when a whole battalion of machine-gunners marched into town. From the moment they arrived it has been one interminable line from morning until night, demanding the Three C.s,—chocolate, cookies, and cigarettes. Luckily my closet was well stocked and so has stood the strain. And speaking of closets, I have acquired a skeleton in mine. It came about through a sick soldier, an accommodating captain and an egg-nogg. The sick boy I discovered in Madame the Caretaker’s stable while breakfasting this morning. He was very miserable, Madame told me, and had been quite unable to eat a thing for days. I stopped in at the stable and verified her words. The boy looked wretched. “Come to the canteen at ten o’clock and I’ll have something for you to eat,” I told him. Then I begged a cup of fresh milk from Madame. The Captain I discovered in front of my canteen counter, and knowing him to be a southerner and a gentleman, I summoned my courage and whispered a petition for a few drops of something, from the flask he carried in his pocket, to put in the egg-nogg for the sick boy. The Captain, who was corpulent and dignified, in some embarrassment replied that he was unfortunately without anything at present, but that the lack would be immediately supplied. He disappeared, returning to produce before my startled eyes, from beneath his coat, a life-sized bottle labeled cognac. Then he invited himself into the kitchen to help make the egg-nogg. He proved expert. I quaked fearing the customers would sniff the cognac through the lattice-work. The sick boy came, turned out to be one of the Captain’s own men. The Captain cocked an unsympathetic eye. “What’s the matter with you, Smith?” he questioned, “been drunk again?” “Captain,” I scolded horrified, “I won’t have any rough talk like that in my kitchen!” Smith indignantly denied the charge. He drank his egg-nogg and left looking three shades happier.
  • 57.
    “Captain,” said I,“did you ever make an egg-nogg for one of your men before?” “Never,” replied the Captain with decision. He drained his own bowl and took his departure. “I will leave the bottle behind,” he told me. “But I don’t want it!” “You might need it again,” he declared. And nothing could induce him to change his mind. That bottle weighs on my conscience like a crime. I have hidden the guilty thing in a corner of the store-room shelf behind some perfectly innocent-looking bundles of stationery and a pile of safety razor blades. But out of sight it continues to haunt my mind. I feel as if I were giving sanctuary to the devil. And, worst of all, I have a vision of coming into the hut some day to find that the bottle has been discovered and the whole Y. M. C. A. is on a jag. Mauvages, November 11. It isn’t true. It isn’t real. It can’t be that the war is really ended. This morning I awoke to the sound of the most tremendous barrage I have ever heard. At this distance however it was almost more like a sensation than a sound, a sort of incessant thrilling, throbbing vibration. The question was on everybody’s lips: “Do you suppose they really will sign the armistice?” “It don’t sound much like peace this morning!” would come the dubious reply. We have heard rumours just since yesterday, but in rumours we have so long ceased to put any faith! As the morning wore on our skepticism grew. The almost unbroken reverberation frayed the nerves. As eleven o’clock drew near the tension became torture. Would the guns cease? Could they? It seemed as if they must go on forever. The clock in the old grey church tower began to strike the hour. I flung open the kitchen door. We all stood breathless, frozen, listening. Ding-dong, ding- dong; through the notes of the bell we could still hear the throbbing of the great guns. Eleven times the slow bell chimed, there was a
  • 58.
    heavy boom, onemore, and then absolute silence. We stared at each other blankly incredulous. “They’ve signed,” said a boy. I walked down the little lane that leads to the ammunition dump and picked a bunch of orange-scarlet berries. I wanted to be alone, to listen. It was a day all pearl and lavender, a violet mist hung over the brown hill-sides. No one passed on the road, there was not a sound of any sort that reached me, the world seemed to be asleep. The stillness was terrifying. I waited, tense, not able to believe, expecting every moment to have the silence broken by the resumption of the cannonade. Then as the minutes passed and still my strained ears could not catch so much as a whisper, I turned back and entered the little roadside Chapel in the Bush. There in its dim blue and silver solitude I knelt down before the little statue of Jeanne d’Arc and prayed. At noon someone started the old church bell to ringing, it jangled frantically for hours. I think we are all a little dazed. I for one have a curious feeling as if I had come up suddenly against a blank wall. Mauvages, November 12. Last night we celebrated. The whole ordnance camp got out and set off flares and signal rockets from the dump, while two of the boys put over a barrage with the machine-gun on the hill. And there was much champagne. This morning the street is hung with flags,—I never knew before how thrilling the tricolor could be until I saw it like this, against the stone-grey of the old houses. A company of French cavalry is just passing through town. They are very beautiful to look at, with their bright blue uniforms, their bright bay horses, and the long slim lances which they carry in one hand, each with a tiny pennant at the end. As each one comes into view down the street I think; “Thank God, for one more Frenchman left alive.” The boys have already begun to argue about the date on which they will reach home. But though the fighting may be over, there are long months still ahead of us here I am sure. And now with the
  • 59.
    strain and theexcitement gone, France is bound to look greyer and muddier and more whats-the-use to the boys than ever before. May Heaven help us all! Mauvages, November 17. I want to make you acquainted with Bill and Nick, my two invaluable assistants. Bill is my official detail formally assigned. Nick is a volunteer, his services a free-will offering proferred at such times as he is not required in his regular capacity as guardian of the bath- house. Bill is a lame tame giant six feet two and up. He slipped a cog in his knee one time while shuffling shells last summer and never got quite straightened out again. Bill is my salvation. He redeems what would otherwise be a desperate situation. For Bill has a Business Brain. If it weren’t for that, I believe I should be driven to the mad- house trying to balance the francs and centimes at the end of each week. Besides having a head for figures, Bill is an all round handy man with a turn for inventions. When I come back to the hut after a morning expedition to Gondrecourt in quest of suppplies, I may or I may not find last night’s dishes washed but I am pretty sure to find some wonderful new contrivance added to my hut equipment. Bill has made me a stove-pipe out of a German powder can. Bill has installed an automatic closing attachment for the main door, which consists of a rope, a pulley, a stove grate and an excruciating squeak; the chief advantage of this invention being the squeak which always betrays the sneak who tries to escape undetected in the middle of a prayer. Sometimes I think it hurts Bill’s pride to have to take orders from a lady, especially one with such an unmathematical brain as I. Occasionally he lapses into a you’re-only- a-little-girl-after-all sort of attitude and then I have to put on all my dignity and read the riot act to him. But when I hand in my weekly cash sheets at Headquarters and the cashier there tells me that my accounts are the best in the whole area, why Bill could have the whole hut and everything in it.
  • 60.
    As for Nick,if Bill is right hand man, why Nick makes a quite indispensable left, and this in spite of the fact that the poor fellow is almost blind. He got a crack in the back of his head from the corner of a case of “75s,” while unloading ammunition some two months ago, which affected the optic nerve. And though the doctor promises a partial restoration of his sight, at present he must grope about in dark glasses and semi-darkness. Nick has a history. An orphan, educated for the priesthood, he ran away at the age of sixteen and started on the career of a cowboy. After having broken every bone in his body in the course of his broncho-busting he rose to the heights of his profession and joined Buffalo Bill’s Wild West Show. Here he met his wife, a lasso and pistol expert. While riding an “outlaw” in Madison Square Garden, he was thrown and had one of his legs badly smashed, which forced him to retire from public life. After this he spent a couple of years as a bar-tender in New York. In his spare moments, aided by his ecclesiastical Latin, he learned practical chemistry from an old German druggist who kept shop next door. Now in his civilian capacity Nick is consulting chemist for a Brooklyn laundry concern, while his wife conducts successfully a French millinery store in Flatbush. So much for romance! Nick is, I am quite sure, the politest Irishman in France. Moreover he is the darling of the feminine portion of the town. Partly by reason of his blindness, which appeals to the quick sympathies of the Frenchwomen, and partly because of his unvarying courtesy, his kindliness and his quaint humour, he is the most sought-after man in Mauvages. He knows, I should judge, some six words of French, but with these he manages to “get by.” And he is forever being invited out to supper. Every morning between sweeping up and washing the dishes and waiting on the counter we hold a coffee party in the kitchen; Bill and Nick and myself and whoever else happens to be around. The party consists of coffee with plenty of sugar and canned milk,—always a treat in the army as in the messes you must drink it plain;—and K. P. cookies. Now K. P. cookies, you must understand, are cookies from the end of the package that the mouse didn’t eat. As there is considerable activity on the part of the mice these days there are
  • 61.
    any number ofK. P. cookies. And yet I have done my best. Pricked on by conscience I said to Nick day before yesterday, “Nick do you suppose you could get me a trap?” “Certainly Ma’am, I’ll buy one at the store.” “But wait a minute, do you know the word for mouse-trap?” “Don’t worry. That’s not in the least necessary.” And he set out for the General Store Articles Militaire down the street. But for once his sign language failed him. He was offered everything in the store from a screw-driver to an egg-beater and only achieved the trap finally by stumbling over one on the floor. It was a French trap to be baited with flour and sewed up with thread; I looked at it skeptically, but the next morning we had caught a mouse. However today it was K. P. cookies as usual. “Bill,” I said, “you’ll have to borrow Iodine.” Iodine is the Medical Sergeant’s cat. “Aw shucks,” says Bill, “Iodine is a frog cat. She wouldn’t look at a mouse unless you served it to her on a platter dressed with garlic.” Bill says no home is complete without a dog. I quite agree with him. Only, I say, we must catch him young so we can bring him up in the way he should go. These French dogs for the most part seem to have neither manners nor morals. So Bill is keeping an eye out for a likely puppy. “But,” he said, “when we close up here, the only way we’ll be able to settle it between us will be to make him into sausages.” If we ever do get a dog I think I shall call him “Tin Hat” just because every other dog in the A. E. F. is named “Cognac.” Mauvages, November 20. Our relations to the French populace are enough to try a diplomat. Hardly a day passes in the hut but what some delicate social or ethical problem arises. First, there is Louis, a most disreputable old scamp if there ever was one. He keeps the café across the street and so is my deadly rival. The other day the old rascal appeared at my counter grinning from ear to ear, and demanded “bonbons pour le rheum,” producing,
  • 62.
    in witness ofhis urgent need, a feeble and patently artificial cough. When I answered that unfortunately we had none, he instantly substituted chocolate in his request. Unable to resist the rapscallion’s grin I gave him a handful, whereat in beaming gratitude he immediately invited me over to the café to have a glass of wine at his expense. And when I hastily informed him that I didn’t care for wine he genially amended the invitation so that it stood, “glass of beer.” And now I am told by the boys that he has announced that I, forsooth, am his “fiancée!” But chiefly there is Rebecca. We call her Rebecca because when Bill goes to the well to get a pail of water he usually happens to meet her there. Rebecca is thin and dark and lively. Her English vocabulary includes such phrases as “beeg steef” and “Mek eet snappee!” She is, as the boys put it, “full of pep.” Rebecca has a little black and villainous-looking husband who occasionally appears in town from the trenches, but for the most part she is free to follow where her fancy leads. If it should ever lead her to confession I am afraid she would make the old Curé’s eyebrows curl. Bill’s acquaintance with Rebecca is entirely on business lines he wants me to understand. She does his laundry for him. “It’s all very well,” I say, “to take her your washing, but why must you take her chocolates?” He knows I disapprove. When he lingers too long on the water detail I eye him severely on his return. “Bill, have you been hobnobbing with Rebecca?” Bill grins admission. Rebecca lives in a white little one story door-and-window house just around the corner on the Rue d’Eglise which I must pass going to my canteen. And Rebecca keeps tab on the precise hour and minute at which I return to my billet under Bill’s escort every night. Going home one stormy night I took Bill’s arm. The next day Bill informed me that Rebecca had advised him that such conduct, according to French notions, was not quite comme il faut. Bill, I find, is able to make an astonishing amount of conversation with his “nigger French” that takes absolutely no account of moods, tenses, conjugations, declinations or any of the other stuff in
  • 63.
    grammar books. AndI am afraid he understands a great deal that it would be just as well he didn’t. “How did you learn it all?” I asked him. He looked at me side-wise. “Rebecca gave me lessons,” he answered grinning. Last night, as we passed Rebecca’s house, I noticed that her door was the least bit ajar. As Bill left me at my gate I admonished him; “Now don’t you stop to say good-night to Rebecca.” “Gosh, no!” said Bill, “if I did I’m afraid I might have to hurry or I’d be late for breakfast.” Whenever I meet Rebecca on the street she always bows to me most urbanely. Nor is Rebecca all my concern in relation to Big Bill. There is also the pretty girl who lives down the street who undoubtedly would not be averse to accompanying him to America. Bill stops at her house every night in order to get a quart of fresh milk for the C. O.’s breakfast. I bid him be wary of these Franco-American alliances, citing horrible examples I have known, such as the machine-gunner, for instance, who, in order to be in harmony with his future family- in-law, felt it incumbent on him to appear at his wedding wearing a pair of wooden shoes; and of the doughboy who married a widow with two children, and, since he knew no French and she no English, persuaded his company commander to detail an interpreter to live in the house with them for the first three days after their marriage. Not many days ago a girl came to my kitchen door in company with a soldier. She had a United States paymaster’s cheque which she wished to have cashed. Afterwards I questioned Bill. It seems a lieutenant had married and afterwards divorced her. She was still drawing his allotment. She looked so thoroughly the peasant, bare- headed, in a shawl and shoddy skirt, with nothing to particularly distinguish her pretty but inexpressive face, that I voiced my wonder to the boys. “Oh but you ought to see her when she gets dressed up!” they said.
  • 64.
    “Fine feathers don’tmake fine birds,” I remind severely. “Bill, be warned!” “Yes, but there’s Gaby,” Bill suggests. “What about her?” Now Gaby is the little chauffeuse who has been driver for a French general three years and who turns up periodically in town. She is quaint as a wood-cut and solemn as an owl, with her shock of bobbed hair and her great staring child-like eyes. She sits at the mess table and never says a word but draws your glance irresistibly. Always she wears an odd little straight-cut dress hanging just below her knees and a croix de guerre pinned to her breast. Gaby killed a man with her car not long since and was held a prisoner at Ligny-en- Barrois for ten days in consequence. Gaby and one of the sergeants at the A. R. are undergoing all the woe and wonder of love’s young dream. “Oh well,” I say, “Gaby is different.” This afternoon Rebecca appeared at the canteen and asked for Bill. She was so elegantly attired that at first I didn’t know her. After a parley at the door, Bill, with an odd expression on his face, takes his second-best raincoat from the peg and hands it to her. I looked my inquiries. An old doughboy sweetheart of the lady’s, it appears, had returned on leave and they were going travelling together. “Going off on a honey-moon with another feller, in my raincoat! Gosh, it’s a cruel war!” grinned Bill. Mauvages, November 24. Now that the time is drawing on toward Christmas the boys,— bless them!—are all wanting to send some remembrance to mothers, sisters, wives and sweethearts at home. But what to send has been the desperate question. One sort of goods and one only is offered for such purposes by the French stores in this locality, a line of flimsy silk stuff, handkerchiefs, scarfs and little aprons, machine- embroidered with gay flowers and each bearing the legend “Souvenir de France.” They are fragile slazy things, absurdly high- priced, inappropriate and often hideous. But to the boys they are altogether beautiful. After many requests and inquiries I gave in. I
  • 65.
    went to Gondrecourtand purchased what I could find that was the least tawdry, the least exorbitant. I brought them to the canteen; they proved so popular that three days afterward I had to make another trip to town to buy some more. Now we carry a regular stock of fancy silk handkerchiefs and aprons in addition to the chewing tobacco and cigarettes. But here one is faced with a delicate problem. Each handkerchief is embroidered with some such specific legend as To my Sweetheart, To My Dear Wife, To my Darling Daughter,—I refused to consider the bit of lacy frippery marked To my Dear Son!—and this complicates matters immensely I find. Somehow we always manage to have a supply of Sweethearts on hand when a man is in quest of a Dear Wife and vice versa. In vain I artfully suggest that it would be a pretty compliment to call one’s wife “Dear Sweetheart,” to their minds there seems to be something essentially compromising in such a notion. Occasionally the reverse will work however, and a boy, grinning and abashed, will select a handkerchief marked “Dear Wife” to send to his sweetheart. Sometimes during these sales one’s faith in the single heartedness of Young America receives a shock, as when an innocent-looking lad will blandly select half a dozen “Dear Sweethearts” and put each in a separate envelope to send to a different girl! Speaking of souvenirs, there is a boy who acts as fireman on the dinky little engine that pulls the work-train on the narrow-gauge between Mauvages and Sauvoy. He belongs to a regiment of engineers who served with the British in Flanders for some eight months. While there he dug up enough dead Germans,—“You could always tell where they were buried because the grass grew so much greener there,” he explained,—and picked enough gold fillings out of their teeth, to make a whole match box full. He was going to take it home and have a dentist put the gold in his teeth “for a souvenir,” but unluckily in the spring drive he lost all his possessions and the match box with them. Now this, as Kipling would say, is a true story. Mauvages, November 30.
  • 66.
    Let me recountto you the gentle tale of the German prisoners and the Thanksgiving movies, an incident which I consider a sort of sermon in a nutshell and a Warning to the Nations. Unluckily there is in this division a secretary who is a sentimentalist. He has an idea that an important part of his object in France is “to enliven the long evenings of the French villagers,” and particularly does he consider it his Christian duty to do something to demonstrate how much we love the poor German prisoners, those gentlemen who wear the big P. G. for Prisonnier de Guerre on their backs and “ought,” as the boys say, “to have an I in the middle.” There are several hundred of them in a camp at Gondrecourt and they are, it is said, just as well housed and fed as our boys, and not made to work nearly as hard. Now, as there was no other sort of entertainment available, I had set my heart on having movies in my hut on Thanksgiving. I had presented my request at the Headquarters office and understood the matter settled. But the Sentimental Secretary it seems had made up his mind that the poor dear German prisoners must have a treat and, other schemes falling through, he also put in a request for the movies. There was only one portable machine in working order. Through some misunderstanding or something in the office, the P. G.s got the movies. To enlarge upon my sentiments when the news was broken to me Thursday morning or to record the opinions expressed by the boys in regard to the matter, is not to the purpose of this tale. Failing our show, all that I could manage in the way of celebration was a little box of nuts and raisins tied up with a bit of red, white and blue ribbon for every man in camp. The mess sergeant, however, outdid himself. Our Thanksgiving dinner was nothing less than a feast. For days the A. R. jitney had been scouring the country for poultry. At last the sergeant had succeeded in getting enough for all. He did this by assembling specimens of the whole feathered tribe; turkey, duck, chicken and goose. And I had a slice of each. But for all that I didn’t enjoy that dinner worth six-pence. Those movies were on my mind. I tried to think of the touching gratitude of the German prisoners. Perhaps after all if one should pursue them with
  • 67.
    delicate attentions itmight lead them to see the error of their ways. Perhaps giving them a movie show would inculcate, by example, a beautiful lesson of Christian charity and forgiveness. Who could tell what uplifting moral influence Charlie Chaplin or Mutt and Jeff might exert? Last night was our regular movie-night. In the midst of preparing for the show, Georges, the French operator, who was getting the machine ready, Georges the little dandy, always nonchalant and blasé, came charging back to the counter, his eyes as big as arc- lights. He thrust his hands, which were full of cartridges, beneath my nose, fairly dancing on tip-toe in his excitement. He had found them in the carbide; when the carbide had gotten hot, “Poof!” he dramatized the wrecking of the hut with explosive gestures. “C’est les Boches! Les cochons!” Never again would he take his machine there, never, never! As the machine had been left at the German Prison Camp after the Thanksgiving show and then brought directly from there to Mauvages there seems little room for doubt that the prisoners had placed the shells there. Of course, if there were any poetic justice in things, the Sentimental Secretary himself would have been blown up by the Germans’ cartridges, but unfortunately in real life things don’t happen that way. Mauvages, December 3. The French Army is in possession of Mauvages. A regiment of artillery moved in on us yesterday afternoon. There seemed a never- ending line of them as they crawled into town, the horses just barely able to drag the heavy pieces. There must have been a shocking shortage of fodder in the French Army; the poor beasts look wretched beyond words. The big guns are lined up all along the street. They look like great spotted lizards in their green and brown and yellow coats of camouflage. Each piece has a girl’s name carved on the muzzle. The one in front of my canteen is Marthe, further up the street stand Lucile and Marie. We watched them as they brought the guns into place, unhitched the teams and made their
  • 68.
    preparations to settledown and stay. Once settled, our perplexities began. Immediately they started to trickle into the canteen in search of cigarettes. To the first comers in a weak moment I slipped a few packages. That was enough. Thereafter it was just like flies to the molasses jar, and then of course I had to harden my heart and say no. But they wouldn’t take no for an answer. They begged, pleaded and cajoled. I posted a polite sign at the end of the counter explaining how the canteen supplies had been brought into France without payment of any duty, under the strict agreement with the French Government that they would be sold only to Americans. But they refused to read the sign. One handsome brigadier stopped me on the street in order to present his petition. And at the canteen a little poilu with a round cherubic face, after being refused some nine or ten times over at the counter, followed me out into the kitchen to urge his piteous plea. It was dreadful, it was harrowing. I have never felt quite so mean about anything in all my life. In the evening we had billed a stereoptican lecture on London. Forseeing that the poilus would form a large proportion of the audience, I tried to get an interpreter to explain the pictures in French to them but at the last minute the interpreter failed me. Notwithstanding, the Frenchmen remained courteously quiet while the lecture lasted. But once it was finished the atmosphere of the hut underwent a change. The blue-coated figures who were swarming into the canteen now had evidently spent the earlier part of the evening in the cafés. I went out into the centre of the hut to see what was going on; all about me stretched a swarm of poilus in a genial mood. The door squeaked open, a little soldier came skipping into the hut. To my horror I saw he carried in one hand a tall tumbler and in the other a large bottle of Benedictine. The victrola was jigging out a rag on the counter. Posing for a minute in an attitude reminiscent of the great Isadora, the little poilu proceeded to dance in time to the music, pirouetting on one toe as he waved the bottle and the tumbler above his head with Bacchanalian gestures. Then suddenly he sat down at one of the tables and started to pour himself a glass. I swooped down upon him. It was défendu I explained, strictly and absolutely défendu to
  • 69.
    drink in thishut. He stared incredulous. I reiterated with emphasis. Finally he nodded sulkily and, slipping the bottle underneath his arm, turned away. Two minutes later I caught him offering a red-nosed friend a drink square in front of my counter. I flew to the attack again. I told him it was against the rules to so much as bring wine into the hut. He held his ground defiantly. I wanted to take the little wretch by his coat collar and march him out the door; I felt I could have done it. Instead I plead, expostulated and commanded. A score of grinning poilus crowded about us: it was evidently as good as a show to them. I entreated the little poilu please, please to carry the bottle out of the hut! “Dehors! Dehors! Outside!” they chorused gleefully. I exhausted my vocabulary, apparently without effect. The little poilu wasn’t used to taking orders from a girl, especially one who spoke French so badly, but finally I won. “Bon!” he snapped explosively, turned on his heel and marched out. I fled precipitately to the kitchen and stayed there until closing time. I didn’t feel equal to coping with any more tipsy poilus. It’s curious how the whole character of a dwelling-place can change. When the priest and the cat and I are keeping house together, the old mansion is the dimmest, most decorous place imaginable. At night I let myself in the dark front door, locking it carefully behind me,—Monsieur scolded me for leaving it unlocked once; I had left him, he said, at the mercy of the passersby!—then grope my way down the cold unlighted hall and up the steep stairs to my chilly room and to bed by one flickering candle’s light. The place is as silent and lifeless as a tomb. Then new troops come into town and suddenly everything is changed. The lower floor is taken over for an officers’ mess and often too, for Headquarters. Savory odors of cooking, warm smells mount up the dim stairway, candles gutter in niches in the passage-ways, smart-looking officers in khaki or horizon-blue as the case may be, meet and salute one in the hall. The tramp of booted feet, the ring of spurs, the clink of glasses, laughter, song, the piano played tumultuously sometimes late into the night,—everything from Madelon to Mozart—and most startling, and incredible of all, the jangle of a telephone bell, installed for the
  • 70.
    occasion; for afew days we live in a strange bustling vivid world, then on they move and we are left again to our silence and solitude. Tonight as I was washing up for supper I was startled by a rap on my door. There stood Monsieur le Curé and a French officer. I had a bad moment wondering what the cause of such a visitation might be. Was he going to turn me out of my billet perhaps? Or was he going to complain about the treatment his men had received in the Y.? Monsieur le Curé was ambling through a long and elaborate peroration. At first I could make no sense out of it, then suddenly I caught on. Monsieur le Capitaine was a stamp collector. He wanted to know if I perhaps had some stamps des États-Unis which I could spare him! Reports have come in tonight of friction between the French and American soldiers in town, resulting in a number of scrimmages. The whole trouble springs, I gather, from the eternal feminine and the native jealousy of the male; the Fair Sex of Mauvages having made quite evident to the poilus their decided preference for the doughboys. Mauvages, December 6. The theatrical season at Mauvages has been inaugurated. The carpenters were busy in the hut all day yesterday, hammering and sawing, making us a roll curtain out of roofing paper, manufacturing foot-lights from commissary candles and tin reflectors cut from the lining of tobacco cases. When the stage was done it was very gay. We had a red curtain across the back, bright yellow wings, red and yellow draperies around the proscenium arch, festoons of little flags strung across the top, and a large American flag draped centre back. It wasn’t what we wanted, it was just what, by hook or crook we could get, and the effect really wasn’t half as bad as it sounds. The programme might be classed in two parts, rehearsed and impromptu. For a starter we dropped a tear over Baby’s Prayer, that bit of ninety-nine one-hundredths pure sentimentality, without which no programme in the A. E. F. is complete these days; after which we were adjured to “Pray for sunshine, But always be prepared for
  • 71.
    rain,”—a quite superfluousadmonition in this part of France at this season of the year! “Put all your pennies on the shelf, The almighty dollar will take care of itself.” “Humph!” grunted the boy next me, “I’ll bet it was a Jew wrote that.” Following the songs we heard Barney, the Poet Laureate of the Camp, celebrate the deeds of the ordnance detachment in verse. At least we supposed that was what it was, for Barney has a brogue all his own and if you get one word in ten you’re lucky. As the C. O. says, it is much easier to “compree” a Frenchman than it is to understand Barney. After Barney we had a sermon, a burlesque darky sermon preached by a black-face comedian. As luck would have it, two real darkies from a labor camp up the line slipped in at the back of the hut just as the preacher began. They took it all in deadly earnest, and warmed, I suspect, by a glass at the corner café, they presently began to respond to the preacher’s exhortations with genuine religious fervor. “Dat’s so! You tell ’em bruder! Hallelujah! Bless de Lord!” The audience up front, hearing a commotion and unluckily not catching the comedy, hissed indignantly and the darkies, abashed, slunk out. Of course at the last moment some of our headliners failed to come across. The mumps claimed our dramatic reader and our buck- and-wing dancer sent word, just as the curtain was going up, that in all the camp, no shoes outside of hob-nails, large enough for him could be found. But we made up for these defections by our impromptu acts. The most surprising of these was the Little Fat Poilu. He popped up suddenly from Heaven knows where, a round rosy dumpling of a man with a shiny nose and a fat black beard, and offered his services. On his first appearance he played the violin with vim and spirit. Then in answer to the applause he dropped his violin, seized the tall hat from the head of the darky preacher, clapped it on his own, and bounced back onto the stage. The transformation was
  • 72.
    amazing. In aninstant, instead of a poilu he had become a jolly little bourgeois shopkeeper out for a stroll on the boulevard. He proceeded to sing a comic song, a song with an interminable number of verses, unquestionably very funny and in all probability quite scandalous. The French portion of the audience was charmed, they joined vociferously in the jiggy choruses, and when he had done they insisted on another and another. For a while it looked as if France was going to run away with the programme, but finally the little poilu came to the end of his repertoire,—or of his breath maybe, and America once more took the stage. Today we are living in an atmosphere of theatrical enterprise. Already there are three or four “bigger and better” rival shows in process of incubation. What’s more, Barney is writing a play. He sits at one of the canteen tables surrounded by a group of admiring would-be actors and each sheet, as he finishes it, is gravely handed around the crowd. So far it seems to contain just three characters; Rose the beautiful stenographer, the villain landlord and the office boy. I am waiting in suspense to see whether Barney’s masterpiece is going to turn out a melodrama, a problem play or a dramatic treatise on the social and political wrongs of Ireland. The French troops are moving tomorrow. Tonight the Little Fat Poilu came to bid us good-bye. When no one was looking I filled his pockets up with cigarettes. Mauvages, December 9. A very regrettable incident occurred last night. The day being Sunday we were due for a religious service at seven-fifteen. At seven-ten the Reverend Gentleman, who was to instruct my flock in the way wherein they should go, arrived in company with the Business Manager from Gondrecourt. Now it happened that the Reverend Gentleman on this occasion was none other than my friend the Sentimental Secretary. He surveyed the congregation; there were nine boys in the hut. He sat down and waited for the audience to arrive. But the audience didn’t. Instead one wretch surreptitiously sneaked out the door. At last I felt it necessary to come forward with
  • 73.
    apologies and explanations;my flock at present was small to start with, the sheep had all gone to Domremy on an excursion, the goats were deep in an after-payday poker game. “Do you wish me to hold the meeting?” the R. G. questioned grimly. “If you will.” The Reverend Gentleman, a bit tight about the lips, laid on. It was a cold night; we gathered by the fire. I tried to make myself look as large as possible, but stretch the congregation as you might, we only reached two-thirds of the way around the stove. “Well,” said the Business Manager when it was all over with, “how soon will you be ready to close out this hut?” I reminded him that after all it would have only taken ten righteous to save Sodom, so might not eight save Mauvages? Of course just as soon as the Reverend Gentleman and the Business Manager had shaken our dust off their feet and disappeared, a whole crowd of boys came streaming into the hut. I accused them of having waited just around the corner until they had seen the Religious Service depart. As for Big Bill I consider him nothing short of a slacker, he sat in the kitchen all evening and wrote a letter to his girl. I tell him that as hut detail it is obviously his duty to attend all services but he explains that “it makes him homesick.” In a town on the road between Mauvages and Gondrecourt there is a labor camp of Chinese coolies. These are the laziest folk in Europe I am sure. They are supposed to be working on the road, which needs it badly enough, resembling, as one boy declared, “the top of a stove when all the lids are taken off.” All day long they squat by the roadside, or stand idle watching the traffic go by. “They’d rather be caught dead than caught working,” as one boy said. The story goes that if one of them dies the French Government must pay the Chinese Government thirty francs. They come dear at that. Moreover, they are unconscionable thieves. Up on the hill back of the town where they are billeted there is an American aviation field. The camp was abandoned after the armistice, but twelve boys from the air service were detailed to stay and guard the property. These boys
  • 74.
    find that thechief end of their life is to chase the Chinks out of the stores; they are quite persistent and perfectly unabashed. More than that, if the Chinks catch one of the guards by himself, they are likely to attack in force armed with sticks and as our boys are not allowed to carry weapons, such an attack is no laughing matter. The trouble began, the boys tell me, in the days when the camp was populated; two mechanics had once thought it a good joke to give one of the Chinks a bath by ducking him in the horse-trough. One of these heathen, I am told, came to church here at Mauvages yesterday and almost broke up the meeting. It pleased him to sing all the way through the service, a wierd sing-song chant all his own, and as if that were not bad enough, in the middle of a prayer he had turned square about and started to play with the rosary of the scandalized Madame behind him! The most pious- minded could scarcely keep their thoughts on the priest’s dissertation. There was “beaucoup distraction” as one Mademoiselle phrased it. This morning I went down to Gondrecourt. “Well, and how are your eight men?” asked the Business Manager. “One of them has gone to the hospital with the mumps,” I answered. “So now I have seven.” Mauvages, December 12. I have been A. W. O. L. I have been on a joy ride. For the first time since I came to France I have taken a real day off. I got a chance to go up to the old battle front on a “speeder.” I didn’t mention the matter to the office, but I took the chance. I knew I could safely trust the hut to the management of Bill and Nick for one day. We started out shortly after six A. M., on the narrow-gauge bound for Mont Sec. There were five of us on the speeder which is, you must know, a little flat car something like a hand-car, only that instead of being propelled by hand power, it is run by a gasolene motor. Speeders are the jolliest possible way of travelling and they can go like the wind: they possess just two disadvantages, their
  • 75.
    propensity for havingengine trouble, and the ease with which they jump the track at the slightest provocation. It is told how in Abainville the other day a speeder jumped the rails, the engineer, after turning a half a dozen somersaults, picked himself up, squared off, demanded; “Who in hell put the pebble on the track?” From Mauvages we followed the A. and S. to Sorcy. There we switched onto the line which the boys at Abainville used to declare “ran through the trenches.” They would tell me wonderful tales of the trips they had taken on this line; the smoke-stack of the engine protruded over the top, they explained, and “Gosh, you could hear the bullets just splatterin’ against it!” A short ways out from Sorcy we passed the last inhabited village. Ahead of us we could see the barren sinister outline of Mont Sec, that little Gibraltar of the land which the Germans had captured and fortified early in the war, which the French had endeavored to retake in 1915 with the most fearful losses, but which had remained impregnable, commanding, looking down in contempt on our men in their muddy lowland trenches of the Toul Sector, until, on September twelfth, the American Army had taken it along with the rest of the Saint Mihiel salient. As we neared Mont Sec we began to pass devastated villages, some of them mere formless ruins, others from a distance holding the shape and outline of habitable dwelling-places but on approach revealing themselves as mere groups of riddled house-shells. Across the open places stretched interminable grey swathes of rusting tangled wire, “barbed-wire enough to fence Texas,” as one boy put it. On sidings we passed long lines of cars full of salvage, all the junk of war tossed carelessly together. Along the tracks were scattered empty shells and here and there piles of unexploded ammunition. In a shell-hole by the roadside, half filled with water, lay a hob-nailed shoe,—prosaic but pitiful witness of some tragedy. It was the loneliest land, the most forsaken I have ever seen. Far and wide as one looked over the empty plain there was no living, moving creature anywhere. At the foot of Mont Sec we stopped. There in the woods were the remains of a German camp; it had been a jolly little place fixed up
  • 76.
    like a beergarden underneath the trees, with fancy “rustic” work and chairs and tables. We left the speeder there, and tramping across the fields, climbed Mont Sec. Near the top we found the entrances to the dugouts. The hill was tunneled through from side to side, all the corridors and rooms walled, roofed and floored with the heaviest oak lumber. Everywhere through the passage-ways ran a perfect network of electric wires. Long stairs led to the different levels. No furnishings were left except the bunks and some rough tables. We ate our luncheon of bread, jam and corn willy in what had evidently been the officers’ quarters; the room was nicely finished with cement, there was a fancy moulded pattern in bas relief over the doorway, a pipe-hole showed where a stove had been. After lunch we inspected the concrete machine-gun pill-boxes which dotted the hill-top. Then we went down the steep eastern slope to the village of Mont Sec. About the town, to judge from the ploughed and pitted vineyards, the fighting must have been the fiercest. The village was a village of the dead. We went inside the church; part of the tower, some of the walls, a little of the roof was left, beyond that nothing. Near the door a French officer had scrawled “Maudite soit le boche qui détruit les églises,”—cursed be the Hun who destroys the churches. In this church, Madame the Caretaker tells me, the Germans commanded all the male inhabitants of Mont Sec to assemble. Here they were kept prisoners for three days and nights. On the fourth day they were marched off at the bayonet’s point into Germany, and no one has ever heard a word from them since. Just outside the village in the little cemetery, ploughed with shell- holes, we found French, American and German graves. The German inscriptions all commemorated “heroes dead for the Fatherland;” one of them vowed, with the help of God, vengeance on the enemy. We went back to the speeder. As it was early in the afternoon we decided to go on. Rounding Mont Sec, we passed into German occupied territory. We saw the famous cabbage patches which fed our soldiers after the Saint Mihiel drive, and, on a hillock beside the road, one memorable scarecrow dressed from head to foot as a
  • 77.
    German soldier, “feldgrau”uniform, cartridge belt, helmet and all. At Hattonchatel we looked down on the German barracks from the hill- side but didn’t have time to stop. It was growing late, so we must turn about-face. Once headed for home our troubles began. The rain which had been teasing us all day as a faint drizzle, settled down to business. A few hundred yards down the hill-side the speeder jumped the track. Fortunately we weren’t running fast and the speeder jumped on the right side, if it had jumped on the left we might have gone over the edge of the mountainous hill-side. As it was no real harm resulted beyond a violent bumping and shaking up; I jumped and got a lame wrist. “The chances are, that whatever happens, she won’t turn over,” the boys told me, “so hang on after this.” So I hung tight. The engine, which had worked like a charm all the way up, began to sulk and balk by fits. Presently it grew dark. We had one lantern, we lighted it and the boy who sat at the front end held it so the light would fall on the rails. Every now and then the wind would blow it out. At each station along the track we would stop and ask the engineer operators whether the block ahead was clear. When we came to the last station before the long forest stretches about Mont Sec the operator who came out to speak to us was quite angry; there were three trains, he said, somewhere on the track ahead; we were doing a very dangerous thing, running after dark. We went on, straining our eyes as we entered the woods in order to discern the dark mass on the track ahead which would mean a train, for the trains, in memory of war days, I suppose, carry absolutely no lights. A week ago a speeder ran head-on into a train at night just above Sauvoy; of its three passengers, two were killed, the other fearfully injured. We held ourselves tense, ready the moment we had made out a train, and the speeder slowed down, to jump, and, lifting the car, push it to one side off the tracks until the train had passed. Once we were lucky enough to make a siding just at the critical moment. Sometimes we ran at the edge of high embankments, sometimes we would cross, on a trestle, a wide marshy stream; then the thought would come to me, What if the speeder should jump here? And she did jump twice more on the way back, but luckily both times in well-selected places. The worst
  • 78.
    feature of theseacrobatics was that the jar had an unhealthy effect upon the engine and after each occasion the mechanics in the crowd had to delve and tinker before the speeder could be coaxed to speed again. Also it was wet. The rain soaked through my raincoat, through my sweater, into my leather jacket; my skirt was a dripping rag, the water oozed from my gloves, raindrops dripped from my nose, my “waterproof” shoes were like sponges. You felt, as one of the boys put it, exactly like a figure in a fountain. Between Mont Sec and Sorcy we got a tow. In the dark we came upon the rear end of a salvage train, tied ourselves up to it, and bumped merrily along behind until the train turned off on a branch line and we had to cut loose and make our own way with the increasingly contrary engine. Fortunately, from that point most of the way was down hill; on the up-grades we got off and walked; the last part of the way the boys simply had to push the car. We reached home at half-past ten, tired, soaked to the skin, but happy. Mauvages, December 16. After this, Mauvages is going to be on the map! Mauvages is to be headquarters for the —— Artillery Brigade, with seventeen hundred men in town and thousands more in the villages about. Wonderful to say, this is the very brigade to which my two batteries from the Artillery School belong and though neither of these will be here in town, still they will be near enough so I can get a glimpse of my old boys, I am sure. Already we have an ammunition train and a crowd of “casuals” waiting here for their outfits. The hut, which has of late been rather empty mornings, is now filled all day. These casuals are for the most part replacements, shipped here directly from the ports, after a ten days’ residence in France. They have nothing to do at present but sit in the hut and think how miserable they are. It is funny to hear them talk. Their opinion of Mauvages is inexpressible in polite terms. They are quite convinced that they have come to the Very Last Hole on Earth. In vain I assure them that Mauvages is quite a fine town, as French towns go, in vain I draw their attention to its beauties and
  • 79.
    advantages. They areabsolutely certain that nothing could be worse! Meanwhile I have been busy making frantic trips into Gondrecourt to demand, in view of the coming crowds, a new hut, an electric lighting system, an addition to the old hut, anything or everything, except a man secretary! But Gondrecourt takes the situation very calmly. Just to pass the time away, one of the new arrivals went fishing in the canal yesterday. He bestowed his catch on me; it measured about six inches by one and a quarter. As it was still wriggling faintly I put the poor thing in the water-pail, only to find later that Big Bill in disgust had thrown water and fish out into the back yard. Whereupon I raised such an outcry that Bill must go out in the dark and feel through the wet grass for that fish until he found it. I carried it down to camp, inviting the K. P.s to prepare it for the C. O.’s dinner. At dinner it appeared elegantly garnished with parsley in the center of a huge platter. Just to pay me back they made me eat it, while the rest dined on steak. “How do you suppose he caught it?” asked the C. O. I said nothing. Fishing with hand-grenades is strictly against the law. Mauvages, December 18. Mauvages is in disgrace. Mauvages is the black sheep in the Y. fold. Mauvages is in wrong all the way around. And it’s all because of one Old Gentleman and his ill-timed opinions. The Old Gentleman came out to talk to us yesterday evening. We weren’t expecting him. We were expecting a lecture on the Man Without a Country,—whoever that may be, Jack Johnson or the Kaiser! as the boys say,—by the Educational Department. But then we have almost given up expecting to get what we expect. This is only the third time we have been fooled on the Man Without a Country who appears to be our Old Man of the Sea. The Old Gentleman was brought out in state in the best Y. car by the Big Chief, the Entertainment Department and a driver. The Entertainment Department immediately ensconced himself by the
  • 80.
    cook-stove with aSunday Picture Supplement; the driver retired to a secluded corner to play a game of checkers with one of the boys; while the Big Chief took his stand out front. I for once back-slid scandalously, and, instead of occupying a front seat with a deeply interested expression spread upon my countenance, sat in the kitchen and ate jam and waffles, the waffles which were heart- shaped and crisp and heavenly, having been brought by Nick from his latest supper party. The Old Gentleman stood out by the stove, the stage proving too chilly. There was a crowd in the hut. He put his foot in it at the start. He announced himself as an intimate friend of ex-President Roosevelt. The boys, sniffing politics, grew suspicious, even hostile. He began on the scandal of America’s unpreparedness, from that passed by degrees to the view that Germany was not yet defeated and as a climax called upon the boys to rise and put themselves on record as being willing to stay in France until Kingdom come, if necessary, in order to do the job up brown. The boys did not rise. Instead they heckled the Old Gentleman until he grew as red as a turkey-cock and so indignant as to fairly wax speechless. One of the ammunition train boys, a husky lad who, they tell me, is an old guard house standby, led the opposition. Out in the kitchen you could have heard a pin drop. The Entertainment Department and I sat and stared at each other. The whole trouble as I saw it, was that the Old Gentleman had slipped up on his dates. He was giving them a Before-November- Eleventh speech when it was after the eleventh. It was as if he had quite failed to comprehend that at eleven o’clock on that date the whole psychological outlook of the American doughboy underwent an instantaneous change. His entire mental horizon became forthwith concentrated to one burning point,—the desire which he expresses simply but adequately in the words; “I want to go home!” And not ex-President Roosevelt, nor President Wilson, nor General Pershing, nor anybody else could make him interested in anything that was not remotely, at least, related to that issue. At last the agony was over. The Old Gentleman came back to the kitchen mopping his brow. When he had finished expressing his
  • 81.
    opinion of Mauvages,the driver went out to crank the car. The car was gone. Of course then, everyone remembered having heard a car drive off in the middle of the lecture,—every one that is, but I, I had been too interested in the waffles,—but of course no one had really thought that it could be, etc. A search party was recruited which scoured highway and byway. The M. P.s at Gondrecourt were notified by ’phone. Meanwhile it was ten o’clock, a bleak night and four indignant gentlemen were stranded six miles from home. An ambassador was elected to go and lay the case before the A. R. C. O. The C. O. on his way to bed, instructed the emissary where billets for the night might possibly be had. But the Old Gentleman, upon receiving the information, flatly and finally refused to stay in any billet in town; he would sleep in his own bed or no other. After a nervous interval the ambassador again approached the C. O., this time suggesting the loan of his car and chauffeur. The C. O., aroused a second time from bed, acceeded shortly, the ambassador returned to despatch the unfortunate Bill to camp to break the news to the chauffeur. The chauffeur, who was in the midst of an after-hours poker game, when he recovered from his astonishment, replied (expurgated) that he’d come when he got good and ready, and settled back to his game. In the meantime my four guests by the kitchen-stove discussed in part the peculiarities of the Japanese language, but chiefly the shortcomings of Mauvages. The Chief, however, showed himself a gentleman. He washed the dishes up! And considering that he was a man and a minister and that the light was dim and the water cold, he washed them pretty well. At a quarter to eleven the A. R. chauffeur having presumably forced all the others into bankruptcy, or gone bankrupt himself, drove up to the door and I said farewell to my friends. This morning a rescue expedition was sent out from Gondrecourt. It finally discovered the lost car, none the worse for its joy-ride, in a ditch half-way to Sauvoy. Information has reached me on the side that it was a little group of “hard-boiled guys” from the ammunition train who stole the auto. They were displeased with the Old
  • 82.
    Gentleman’s opinions, andthey made up their minds that he should walk home. So this is how matters stand: I and my hut are in discredit at Headquarters, because my boys stole their car. The Old Gentleman has openly declared that Mauvages is the most unpatriotic spot in France. The A. R. C. O. is disgusted because he was routed twice out of bed in one night. The chauffeur is so incensed at me and mine at having to drive into town at eleven P. M. that he persistently forgets to stop for my daily papers. And the boys are all sore and touchy on account of the opinions expressed by the Old Gentleman in and after his lecture. Such is the happy lot of a hut secretary. Mauvages, December 23. The Big Push is here. Our lawn has turned into a gun park with limbers and caissons elbowing each other under our very eaves. All day the little hut is crowded to its capacity and at night it becomes so full that I am literally afraid it will burst out at the seams. Colonels and captains are forever bobbing up like so many Jack-in- the-Boxes in my kitchen which I was used to consider as a refuge and a sanctum. They have the best intentions in the world; they offer me advice on every subject under the sun from the building of new shelves in the canteen to the frequency with which I should require Big Bill to shave. And quite unsolicited they have given me a detail,—a detail of such proportions that I am swamped. I don’t know how many there are. They never stand still long enough for me to count them. Sometimes there appear to be ten and sometimes twenty. Like the Old Woman who lived in the shoe, I have so many details I don’t know what to do. They are the nicest boys that ever were, if only they didn’t take up quite so much room! Now when I am minded to sit down for a moment to think, my only course is to go into the store-room and sit on a packing-box, and the store-room is very cold. And the worst of it is that they all, from colonel to K. P., have the beautiful idea in their heads that I am not to do any work, but just to be a sort of parlor ornament, and a sweet influence; that I will, in short, like the old man who was afraid
  • 83.
    Welcome to ourwebsite – the perfect destination for book lovers and knowledge seekers. We believe that every book holds a new world, offering opportunities for learning, discovery, and personal growth. That’s why we are dedicated to bringing you a diverse collection of books, ranging from classic literature and specialized publications to self-development guides and children's books. More than just a book-buying platform, we strive to be a bridge connecting you with timeless cultural and intellectual values. With an elegant, user-friendly interface and a smart search system, you can quickly find the books that best suit your interests. Additionally, our special promotions and home delivery services help you save time and fully enjoy the joy of reading. Join us on a journey of knowledge exploration, passion nurturing, and personal growth every day! ebookbell.com