How Do You Connect to a Specific Postgres Database without a SQL Front End?

Problem scenario
From a Linux command prompt (e.g., Linux terminal) you want to connect to a specific Postgres database.  How do you create a table or issue SQL commands to a specific Postgres database from the back-end (the Postgres server itself)?

Solution
1.  First enter the Postgres command prompt by running these two commands:

sudo -i -u postgres
psql

2.  Assuming the database's name is cooldb, run this command:

\connect cooldb;

3.  Run your SQL commands. For example:

create table veryneat(id int, name varchar(40));
insert into veryneat values(1, 'words');

Leave a comment

Your email address will not be published. Required fields are marked *