- Install
sudo apt update
sudo apt install postgresql postgresql-contrib
-
Switching Over to the postgres Account
- Switch over to the postgres account
sudo -i -u postgres
- To access a Postgres prompt
psql
-
Accessing a Postgres Prompt Without Switching Accounts
sudo -u postgres psql
- List all databases
\list or \l
- List all tables in the current database
\dt
- To switch databases
\connect database_name
- Start Postgres service
sudo service postgresql start
- Stop Postgres service
sudo service postgresql stop
- Restart Postgres service
sudo service postgresql restart
- To test your connection using psql
psql -U postgres -W
-
Allowing remote connections
- Open
sudo vim /etc/postgresql/9.6/main/pg_hba.conf
- Scroll down to the line that describes local socket connections. It may look like this:
local all all peer
Change it to
host all all 0.0.0.0/0 trust
- Open
sudo vim /etc/postgresql/10/main/postgresql.conf
- Under the section on Connection Settings, add or replace the line that starts with listen_addresses to respond to all requests:
listen_addresses = '*'
- Restart postgres
sudo service postgresql restart
-
To list all user accounts in the PostgreSQL database server
\du+