From Sinfronteras
Jump to: navigation, search

PostgreSQL, often simply Postgres, is an object-relational database management system (ORDBMS) with an emphasis on extensibility and standards compliance.

Deploying PostgreSQL on a Docker Container

Ver Docker

After installing Docker, you are going to need to download the Docker image for Postgres 9.5. You can do this using the following command:

sudo docker pull postgres:9.5

You can then use the following command to see the list of all the Docker images installed on your computer:

sudo docker images

Setting environment variables: In Linux and MacIn Mac and Linux we can use the following commands in Bash to create environment variables:

export DATABASE_USER=postgres
export DATABASE_HOST=localhost
export DATABASE_PORT=5432
export DATABASE_DB=demo

The Postgres 9.5 image should be displayed in the list. At this point, we need to run a Docker container. Each container is a running instance of an image. We can have multiple containers running of a given image, but this time we will only run onecontainer because we don't need two instances of Postgres. We can create a Docker container with the Postgres image using thefollowing command:


Classic PostgreSQL installation

Connecting to Postgres from SQLectron

After starting a Postgres Docker container, we are going to try to connect to it from an external tool to make sure that everything isOK. The tool that we are going to use is known as SQLectron. You can download SQLectron from:

Sólo hay que descargar el archivo correspondientes (Sqlectron-1.30.0.tar.gz para Linux) y ejecutar (no requiere instalación):


After installing SQLectron and opening it. We need to create a new database connection using the add button:

The SQLectron home page

A form will be displayed on the screen. We need to use the same details that we used in our environment variables and click test to check that they work. We will have a problem because we have set the database name environment variable DATABASE_DB as «demo», and by default, Postgres doesn't have a database called demo

Please note that Postgres is going to be our database server but a database server can contain multiple database instances. Bydefault Postgres contains a database instance named postgres so we will use the default database to connect to the database server for the very first time:

Creating a connection in SQLectron

We can click on the test button to check that we can connect. Please note that the Docker Postgres container must be already running as explained in the preceding section. Once you manage to see Successfully connected you can click on save. After saving the new saved connection should be displayed on the screen:

A saved connection in SQLectron

We need to click on the new database connection to open a new SQL editor. We can then write by hand an SQL statement to create the "demo" database:

SQL editor in SQLectron

After executing the preceding command, we need to disconnect from the database using the top right icon. We need to the return to the screen that allows us to select an existing connection:

This time instead of opening the connection we need to edit it. We are going to update the connection details to use the demo database instead of the postgres database.

Once more we can use the "test" button to ensure that everything is correct. If you cannot connect you should first ensure that the Docker container is running using the «docker ps -a» command. If everything is correct, the status column should show how long the container has been running.

PostgreSQL basics

Connect to PostgreSQL

sudo su - postgres

Defauld password

Set password

sudo su - postgres
\password <username>
\password postgres  # for the «postgres» user

List Databases and Tables from command line

Switching Databases:

\c sales

Listing Tables:


Select from a table:

SELECT * FROM users;

Create Users and Databases from command line

sudo su - postgres

To create a database:

createdb -O user dbname

If we want to create the «twitterclone_db» that belog to the user «postgres»:

createdb -O postgres twitterclone_db

Backup and Restore PostgreSQL Database

Drop database