How to Load or Import a .csv File to a PostgreSQL Table



If you need to populate a PostGreSQL table with data that comes in .csv (comma-separated values) file format, here are the steps to do so:

1.  Login to PostGreSQL with your user that can access and manipulate the table

psql -h dba-works-01 -U dbaworks salesdb

2.  Use the copy command to populate the table with data from .csv file


COPY transactions FROM '/path-to-csv-file/transactions.csv' WITH (FORMAT csv);


NOTE that you can use COPY when you have superuser root privileges.

OR you can use \COPY if you do not have superuser access.


\COPY transactions FROM '/path-to-csv-file/transactions.csv
DELIMITER ',' CSV



If you want to learn about PostgreSQL or update your knowledge about it, you can check out this book below:





CSV Photo by Mika Baumeister on Unsplash

Add a Command Alias to Linux User Profile




Problem:  I'm tired of typing long commands in the Linux command prompt whenever I want something done. Not only that, typing these commands is prone to typo errors.

Solution: Add a command alias to our user profile.

Here's how:

1.  Edit your Linux OS user profile (or .bash_profile)  and add a command alias

dbaworks@dbasrv1:~$ vi .profile

-- add the command alias

# set PATH so it includes user's private bin directories
PATH="$HOME/bin:$HOME/.local/bin:$PATH"

alias check_mongo='ps -ef | grep mongo'


2.  Reload your OS user profile

dbaworks@dbasrv1:~$ . .profile

3.  You can now run the command alias and get the desired result:

dbaworks@dbasrv1:~$ check_mongo

--- output
dbaworks@dbasrv1:~$ check_mongo 
mongod 9655 1 1 Jun29 ? 04:15:13 /usr/bin/mongod -- configsvr --replSet csReplSet1 --port 27050 -f /data/mongod.conf --bind_ip_all 


Learn more about Linux shell scripting using this guide book: