Saturday 18 May 2019

Installing and setup PostgreSQL in Mac

Step 1: Go to the following location and download the executable.

https://postgresapp.com/downloads.html

 

Step 2: Open the executable, it takes you to the setup window.

 


 

Click on Next button.

 

 


You can customize the installation directory (I went with default one ‘/Library/PostgreSQL/14’), click on Next button.

 

 


Select all the components and click on Next button.




You can customize the data directory in this window, I went with default options. Click on Next button.

 

 


Provide the password for database superuser postgres, I configured postgres as the password. Click on Next button.

 

 


Keep the default port and click on Next button.

 


Keep the default locale, click on Next button.

 


 

Check all the configurations in ‘Pre Installation Summary’ window and click on Next button to proceed with the installation.

 

 


Click on Next button to start installation.

 

 


Once installation is successful, you will see following kind of window.


 


Click on Finish button. If you want to install additional postgress tools, drivers select the checkbox and click on Finish button.

 

Post installation, you can confirm whether postgres running or not by querying the processes.

$ps -eaf | grep postgres
  505 30302 30301   0 12:07PM ??         0:00.00 postgres: logger   
  505 30304 30301   0 12:07PM ??         0:00.00 postgres: checkpointer   
  505 30305 30301   0 12:07PM ??         0:00.02 postgres: background writer   
  505 30306 30301   0 12:07PM ??         0:00.01 postgres: walwriter   
  505 30307 30301   0 12:07PM ??         0:00.01 postgres: autovacuum launcher   
  505 30308 30301   0 12:07PM ??         0:00.01 postgres: stats collector   
  505 30309 30301   0 12:07PM ??         0:00.00 postgres: logical replication launcher   
  503 30943 24714   0 12:10PM ttys001    0:00.00 grep postgres

Set the postgres bin directory path to system path

export PATH=$PATH:/Library/PostgreSQL/14/bin

 

Execute the following command to connect to postgres server.

 

psql -U postgres -h localhost

 

Enter the password that you configured at the time of installation.


$psql -U postgres -h localhost
Password for user postgres: 
psql (14.4)
Type "help" for help.

postgres=#

Execute the command help to list the usage of command line interface.

postgres=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

Let’s quit from the command line tool by executing the command \q.

 

Create a database

Execute below command to create database.

createdb -U postgres -h localhost {databaseName}

 

Example

createdb -U postgres -h localhost myOrg

 

Once the database is created, you can login to the database by executing the command ‘psql --dbname=myOrg’.


$createdb -U postgres -h localhost myOrg
Password:

Login to myOrg database

psql -U postgres -h localhost --dbname=myOrg

$psql -U postgres -h localhost --dbname=myOrg
Password for user postgres: 
psql (14.4)
Type "help" for help.

myOrg=#

Create employee table and query

myOrg=# CREATE TABLE employee (id INT, name VARCHAR(20));
CREATE TABLE
myOrg=# 
myOrg=# SELECT * FROM employee;
 id | name 
----+------
(0 rows)





 



Previous                                                 Next                                                 Home

No comments:

Post a Comment