Add PPA Source
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" >> /etc/apt/sources.list.d/pgdg.list.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt update
Install
sudo apt install postgresql-10
Start/stop postgresql service
sudo service postgresql start
sudo service postgresql stop
sudo service postgresql restart
Access psql prompt
sudo -u postgres psql
Type \q
and enter to exit prompt.
Create a new postgres user
- By default postgres comes with a special user named
postgres
but we will not be using this for our web apps. - This user is to perform administration only. So lets create a new user with the help of this user.
sudo -u postgres createuser --interactive --pwprompt
Answer the questions asked.
Create a new database
sudo -u postgres createdb --owner=user_name db_name_goes_here
If you dont have the user_name
as a linux user in your machine then you need to add, otherwise you will get unable to initialize policy plugin
error on terminal. This is called Ident authentication
in postgres.
# create missing linux user
sudo adduser user_name
You can omit --owner=user_name
parameter if db_name is same as user_name.
Login to psql prompt via this user
sudo -u user_name psql db_name
Connect to database
Host: 127.0.0.1
Post: 5432
User: user_name_you_created_above
Password: this_should_be_the_postgres_user_password
Database: db_name_you_just_created
Delete database
sudo -u postgres dropdb db_name
Delete user
sudo -u postgres dropuser user_name
Import/export database
# export
pg_dump -U user_name db_name > dbexport.pgsql
# import
pg_dump -U user_name db_name < dbexport.pgsql
Resources