COPY and CAST() – Bulk uploads in PostgreSQL

Loading data into database tables is pretty much a necessity. Without data, what do we have? Not much at all. The CSV format is super common, used far and wide. I keep a CSV file of my daily walking/hiking stats and am looking to store them in a PostgreSQL database on my local learning/development machine. How can I load a CSV – with several rows of data – at one go in Postgres? What about data types? Any concerns there? Continue reading to see a simple, yet effective solution…

[Head this way for great PostgresSQL blogging >>>]
Advertisements

Function parameters in a PLpgSQL function – How they are accessed…

Having recently published this blog post about PLpgSQL dollar quoting, I thought it appropriate to follow-up with a post on function parameters. In this post, I will demonstrate 2 variations of a simple function, both of which accept a parameter. Yet, how the parameter is accessed and specified is different.

[Head this way for great PostgresSQL blogging >>>]

The STRICT keyword with SELECT INTO – examples in PostgreSQL

In concert with writing quality open-source SQL blog posts, I have developed a yearning to become a well-versed PostgreSQL PLpgSQL programmer. What a perfect opportunity to share what I learn about PLpgSQL, on my blog, while building solid SQL skills. In this post, I will cover the SELECT INTO clause as part of a PLpgSQL function, focusing on the STRICT keyword for even more fine-tuned functionality.

[Head this way for great PostgresSQL blogging >>>]

Dollars and Quotes – PLpgSQL programming basics.

Start talking about dollars, and you will get all the attention in the world. Well, in this instance, it is not so much about dollars in the monetary sense, but more so in a programming one. You’re likely wondering just what on earth I am talking about. Read on and learn with me about a neat feature that PostgreSQL’s PLpgSQL procedural language provides when creating functions and stored procedures.

[Head this way for great PostgresSQL blogging >>>]

REPLACE() string function – with examples in PostgreSQL

String functions are quite useful for manipulating character and text data. Most SQL dialects have several different ones for different use cases so there is surely one (or two) to fit your needs. In this post, I’ll visit the REPLACE() function with examples in PostgreSQL.

Photo by Jason Leung on Unsplash

Note: All data, names or naming found within the database presented in this post, are strictly used for practice, learning, instruction, and testing purposes. It by no means depicts actual data belonging to or being used by any party or organization.

OS and DB used:
  • Xubuntu Linux 18.04.2 LTS (Bionic Beaver)
  • PostgreSQL 11.2


Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!


I’ll use a couple of tables from the PostgreSQL practice DVD Rental database for the example queries below.

A while back, I wrote Useful String Function – REPLACE in MySQL with examples. I feel it’s only fair to provide the same for the PostgreSQL fanboy (in me) and like-minded fans of this great ecosystem as well.

Let’s see what values are present in the ‘film’ table’s ‘rating’ column:

1
2
3
4
5
6
7
8
9
dvdrental=> SELECT DISTINCT rating FROM film;
 rating
--------
 PG
 R
 G
 PG-13
 NC-17
(5 rows)

If you want to change ‘PG’ to ‘Parental Guidance’ you can pass in that argument to REPLACE():

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'PG', 'Parental Guidance')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'PG'
dvdrental-> LIMIT 1;
      replace      
-------------------
 Parental Guidance
(1 row)

As you can see, the syntax is easy to follow. Provide: 1) the target string, 2) the portion of the target string you want to be replaced, 3) what you want that portion replaced with. Then, you’re off to the races.

Here’s another simple example along the same lines as that shown above:

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'G', 'General')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'G'
dvdrental-> LIMIT 1;
 replace
---------
 General
(1 row)

To be honest, I am not quite sure what the NC stands for in ‘NC-17’, but I’ll wing it anyways:

1
2
3
4
5
6
7
8
dvdrental=> SELECT REPLACE(rating::text, 'NC-17', 'Non Compatible-17')
dvdrental-> FROM film
dvdrental-> WHERE rating = 'NC-17'
dvdrental-> LIMIT 1;
      replace      
-------------------
 Non Compatible-17
(1 row)

Try out the REPLACE() function and see how you like it or where you can put it to good use. Hit me up in the comments below with your feedback.

Like what you have read? See anything incorrect? Please comment below and thanks for reading!!!

Explore the official PostgreSQL 11 On-line Documentation for more information.

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.

Have I mentioned how much I love a cup of coffee?!?!

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, is performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.