Welcome to Postgres!
Saturday, 16 April 2022
Read time of 5 min
Postgres
So knowing this how we can use Linux? The answer is ASDF he is one manager for many different languages and CLI, for example, you with him can control your Postgres version for any project one can use postgres 9.1 and others use the 12.0 version. Without any conflict or complication between each other. So for the sake of this article just follow the install documentation for the Postges ASDF Plugin.
So now after everything setup and up running let’s start with the real knowledge. First, we have to understand that posgres came with psql this is one front-end for prompt interaction between the user and the database.
Login on linux with postgres user
By default when you install postgres the user will be postgres
and will be without a password. So to log in, just on your linux terminal access
sudo -i -u postgres
This command will login into postgres user and to access the psql you will need to access
psql
or just use the following command to access psql directly.
Access psql prompt
sudo -u postgres psql postgres
or
psql -U <user-name>
So after that, you will be on psql console, which means you can control the database and the postgres configs. As I said before your user doesn’t have a password yet. So for that create a password with the following command
\password <user-name>
OBS: The default user is postgress
To enable the password request you need to edit de method of security on
sudo nano /etc/postgresql/<your-postgres-version>/main/pg_hba.conf
Search for Database administrative login by Unix domain socket
and change postgres
from peer
to md5
. After that when you try to access psql with your user, the system will ask for the password.
Now if you want to use gpADmin on docker is nice to enable TCP connection beyond localhot for that you need to access:
sudo nano /etc/postgresql/<your-postgres-version>/main/postgresql.conf
Search for CONNECTIONS AND AUTHENTICATION
and dis-comment the line
# listen_addresses = 'localhost' # what IP address(es) to listen on;
and change the 'localhost'
to '*'
. After you change the line will be like:
listen_addresses = '*' # what IP address(es) to listen on;
For the postgres to recognize your change you need now to restart him, for that use the command:
service postgresql restart
or
sudo systemctl restart postgresql
This command can be used to update the config file and fix any bug
Addition of admin extension packs
CREATE EXTENSION adminpack;
OBS: pack of admin tools
Now to create a new user besides the postgres default one, you can use the command
sudo -u postgres createuser -dPs <user-name>
In this command the options were: d - The new user will be allowed to create databases. P - If given, createuser will issue a prompt for the password of the new user. s - The new user will be a superuser.
To know more about creating user options see the createuser doc.
Inside psql
Now a few commands for psql
Get SQL syntax help
\h
Get psql commands
\?
List all users
\du
List connection info
\conninfo
Inside psql as said before is a front-end admin for postgres which means he also can run SQL. And that is another history but he is a few ideas:
Create database:
CREATE DATABASE <db-name>;
Create Table:
CREATE TABLE public."<table-name>"
(
id serial NOT NULL,
name "char" NOT NULL,
PRIMARY KEY (id)
);
Change access for other users
GRANT ALL ON DATABASE <db-name> TO <user-name>;
So that all folks, any questions just get in touch