Configure postgresql on ubuntu using a terminal - linux

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!

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

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

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

Related

how to connect redshift database to a bash script running on ec2 instance

I have couple of sql queries and I want to run them against amazon redhshift , The base script will be on my ec2 instance and will be triggered from there. How do I provide the connection to the database and the run the bunch of sql scripts ? Please guide
The easiest way to do this is to install postgres on the ec2 instance and then use the psql command line tools to connect to Redshift.
Install postgres (yum and apt-get, pick the appropriate one for your Linux type):
sudo apt-get install postgresql postgresql-contrib
sudo yum install postgresql postgresql-contrib
Here's a line from one of my bash scripts that does this. Check out the psql man page for all the options and ways to connect.
PGPASSWORD="$RS_password" psql -a -v ON_ERROR_STOP=1 -h "$RS_DNS" -v schema=public -p "$RS_port_no" -U "$RS_user" -d "$RS_db" -f /tmp/math_$$.SQL || { echo psql command 1 failed ; exit 1; }
There is also a -c option to pass the SQL on the command line.

Facing issues while installing postgresql in Ubuntu

I want to install postgresql in ubuntu but facing lot of issues,searched a lot but still no success,so when I try to install I used below commands:
sudo su -
apt-get install postgresql-9.5
update-rc.d postgresql enable
When the installation completes it gives me the error of
9.5 main 5432 down postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
and after when I give following command
sudo -i -u postgres
psql
it gives me
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
How can I fix this?
In Postgres, you need to connect as a specific user called postgres.
Try doing this : sudo -i -u postgres and then use psql

2 Postgres Servers on local machine. How to uninstall or disassociate one?

Through some mishap I seem to have 2 separate Postgres servers on my local Ubuntu machine. PGAdmin connects to one (localhost:5432), which can also be reached with the terminal through: sudo -u postgres psql postgres -W -h 127.0.0.1 -p 5432
The other "default" server can be reached through the terminal with just sudo -u postgres psql postgres
For other info, locate bin/psql returns:
/usr/bin/psql
/usr/lib/postgresql/9.5/bin/psql
I suspect the latter of those directories is the Postgres 9.5 instance I really want to use, and the one I want to associate with without needing to specify -h 127.0.0.1 -p 5432. The other can be removed completely. How do I resolve this?
You can use the package manager to remove it.
From terminal, you can list the installed versions of PostgreSQL executing:
$ apt list --installed | grep postgresql
The ones with the format postgresql-X (where "X" is the version) is those to remove. You should be able to do it with the apt purge command.
For example, to remove the version 9.6, you have to execute:
$ sudo apt purge postgresql-9.6
/usr/bin/psql should be a symbolic link to the most recent version installed (/usr/lib/postgresql/9.6/bin/psql).

could not access directory "/usr/local/pgsql/data": Permission denied ,while trying to setup database cluster

I am trying to set up Postgres in Redhat. I do the following steps:
$ sudo yum install postgresql-server postgresql-contrib
It is successfully installed. Then i try to set up a database cluster.
$ initdb -D /usr/local/pgsql/data
I get the error:
initdb: could not access directory "/usr/local/pgsql/data": Permission denied
New to linux. Not being able to move forward
Figured out the solution. I went to the specified folder and changed its access permission. It worked after that.
The PostgreSQL documentation notes that the /usr/local/pgsql/data directory needs to be owned by the postgres user.
To do this, you can run the following command:
chown postgres /usr/local/pgsql/data
And if that doesn't work, you might need to also use sudo:
sudo chown postgres /usr/local/pgsql/data
You are running the first command as a superuser and the second as a regular user. Try
sudo initdb -D /usr/local/pgsql/data
While installing postgre, I used
su - postgres
to connect to the specific user with root privileges. Then, the following returned a success
/usr/pgsql-9.5/bin/initdb

How to install postgres-9.1 on Ubuntu 10.04 LTS?

Many months ago, I installed postgres 8.4 using the following command:
$ sudo aptitude install postgresql-8.4 postgresql-client postgresql-contrib
Now I am trying to upgrade to 9.1.
$ sudo aptitude install postgresql-9.1
There apparently is no package that matches 9.1 But it does appear to exist for apt-get. Do I have to go with apt-get?
This may help you out:
http://www.postgresql.org/docs/9.1/static/upgrading.html
or else you can follow these steps:
As root:
su - postgres
pg_dumpall > dump.sql
exit
cp ~postgres/dump.sql /root/
Now you can safely remove the postgresql-8.4 and install postgresql-9.1:
aptitude purge postgresql-8.4
aptitude install postgresql-9.1
Next check the postgresql configuration in /etc/postgresql/9.1/main. If you make any changes, make sure to restart postgres with /etc/init.d/postgresql restart.
Postgresql 9.1 is now up and running, let's import our data back into it.
su - postgres
psql < dump.sql
That's all. You're now fully upgraded to PostgreSQL 9.1
edit:
similar question was asked here:
https://askubuntu.com/questions/66194/how-do-i-migrate-my-postgres-data-from-8-4-to-9-1

Resources