Difference between revisions of "PostgreSQL"

From DUNE
Jump to navigation Jump to search
(Created page with "==PostgreSQL== ===Installation=== <pre> sudo apt-get update sudo apt-get install postgresql postgresql-contrib </pre> You will likely need an additional package if using Post...")
 
Line 1: Line 1:
==PostgreSQL==
+
=Installation=
===Installation===
 
 
<pre>
 
<pre>
 
sudo apt-get update
 
sudo apt-get update
Line 12: Line 11:
 
</pre>
 
</pre>
  
===Running===
+
=Running=
 
If a restart of the DB engine is required:
 
If a restart of the DB engine is required:
 
<pre>
 
<pre>
Line 18: Line 17:
 
</pre>
 
</pre>
  
===PSQL===
+
=PSQL=
====Log in/out on localhost====
+
==Log in/out on localhost==
 
The "psql" client requires a semicolon after each string you enter on
 
The "psql" client requires a semicolon after each string you enter on
 
the command line. It won't report if it's missing and it's easy to forget.
 
the command line. It won't report if it's missing and it's easy to forget.
Line 56: Line 55:
 
Exit out of the PostgreSQL prompt by typing: \q
 
Exit out of the PostgreSQL prompt by typing: \q
  
====Remote Access====
+
==Remote Access==
 
Add or edit the following line in your postgresql.conf, in order to enable access
 
Add or edit the following line in your postgresql.conf, in order to enable access
 
from any host (edit accordingly for more selective access rights):
 
from any host (edit accordingly for more selective access rights):
Line 68: Line 67:
 
</pre>
 
</pre>
  
====Users====
+
==Users==
 
Create a user/role:
 
Create a user/role:
 
<pre>
 
<pre>
Line 79: Line 78:
 
</pre>
 
</pre>
  
===Databases and Tables===
+
==Databases and Tables==
====Creation of DB====
+
===Creation of DB===
 
From the OS prompt:
 
From the OS prompt:
 
<pre>
 
<pre>
Line 91: Line 90:
 
</pre>
 
</pre>
  
====Tables====
+
===Tables===
 
<pre>
 
<pre>
 
testdb=# create table people (
 
testdb=# create table people (
Line 108: Line 107:
 
sudo chmod +x ~/Desktop/firefox.desktop -->
 
sudo chmod +x ~/Desktop/firefox.desktop -->
  
====Info====
+
===Info===
 
List of DBs:
 
List of DBs:
 
<pre>
 
<pre>

Revision as of 18:07, 7 March 2017

Installation

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

You will likely need an additional package if using PostgreSQL as the Django backend:

sudo pip3 install psycopg2

Running

If a restart of the DB engine is required:

sudo service postgresql restart

PSQL

Log in/out on localhost

The "psql" client requires a semicolon after each string you enter on the command line. It won't report if it's missing and it's easy to forget.

Switch over to the postgres account on your server by typing:

sudo -i -u postgres
psql

Same without switching accounts (just switching for one session):

sudo -u postgres psql


After having created a user and making sure authentication method is set correctly in the configuration file (path may be system and version dependent and named something like /etc/postgresql/9.5/main/pg_hba.conf), one can connect to PostgreSQL not as the default "postgres" user but for example as "p3s" or any other userID of choice:

psql -U p3s -d tst

The "-d" option is important because otherwise psql will assume a default database name which may not in fact exist. In the above example, the "tst" database was created beforehand by the user "postgres" to enable testing.

Example of getting help:

testdb=# \h create table


Exit out of the PostgreSQL prompt by typing: \q

Remote Access

Add or edit the following line in your postgresql.conf, in order to enable access from any host (edit accordingly for more selective access rights):

listen_addresses = '*'

To enable authentication from remote hosts for user "foo", edit pg_hba.conf to add

host    all             foo             0.0.0.0/0               md5

Users

Create a user/role:

createuser --interactive

Another example:

create user FOO with SUPERUSER

Databases and Tables

Creation of DB

From the OS prompt:

sudo -u postgres createdb foo

Also can be done from within psql.

postgres=# create database testdb;

Tables

testdb=# create table people (
testdb(# name char(50) primary key not null,
testdb(# age int not null
testdb(# );

Changing a table:

ALTER TABLE foo ADD last_maint date;


Info

List of DBs:

\l

List of schemas:

\d