Trung Tran
Trung Tran

Reputation: 13721

Configure postgresql on ubuntu using a terminal

I installed Postgresql on my virtual machine running ubuntu using the command

sudo apt-get install postgresql-client

Next, I need to set up the connection string, login roles, databases, etc... However, I need to do it in the terminal since I don't have a GUI. Does anyone know how to do this or can point on a tutorial on how to do so??

Thank you in advance!

Upvotes: 1

Views: 2288

Answers (3)

Shubham Batra
Shubham Batra

Reputation: 2375

Follow these command:

Install and Configure Postgres

sudo apt-get install postgresql

This command will install postgres 9.1 by default in ubuntu 14.04 or any debian distribution. if you want to use any specific version of postgres(i.e 9.3, 9.4) you need to update the source list. for example to install postgres-9.4 you can follow below steps. Create the file /etc/apt/sources.list.d/pgdg.list, and add a line for the repository using vim or nano editor

deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

Import the repository signing key, and update the package lists

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

and update the packages using

sudo apt-get update

after that you can search/install the available/supported postgres version

sudo apt-get install postgresql-9.4

After installing postgres 9.4, change to the postgres user so we have the necessary privileges to configure the database

sudo su - postgres

Now create a new database user with access to create and drop database.

createuser --createdb --username postgres --no-createrole --no-superuser --pwprompt odoo

Enter password for new role: ********
Enter it again: ********

Finally exit from the postgres user account:

exit

Upvotes: 2

Mohiul Alam
Mohiul Alam

Reputation: 354

On the server run the commands below.

sudo apt-get update
sudo apt-get install -y postgresql postgresql-contrib

Create a database and a user to access it

I prefer to create one user per database for security.

Replace DATABASE_NAME_HERE and USER_NAME_HERE with the values that you want to use

this will prompt you for a database password

sudo -u postgres createuser -P USER_NAME_HERE
sudo -u postgres createdb -O USER_NAME_HERE DATABASE_NAME_HERE

Test connecting to Postgresql

psql -h localhost -U USER_NAME_HERE DATABASE_NAME_HERE

Postgresql will ask you for your password. Then you should see:

psql (9.3.5) SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) Type "help" for help.

DATABASE_NAME_HERE=>

To exit type:

\q

Upvotes: 1

Jashandeep Sohi
Jashandeep Sohi

Reputation: 5163

man psql or the official psql docs. Also see the tutorial.

Upvotes: 1

Related Questions