8.1 C
Canberra
Thursday, July 24, 2025

The way to arrange pgSQL for Fluent 4?



· 2 min learn


This can be a tutorial for learners about utilizing PostgreSQL. I am going to present you tips on how to robotically backup and restore the database.

NOTE: In case you are already aware of PostgreSQL, however you don’t know a lot about tips on how to use databases in Vapor, it is best to learn my different tutorial about Fluent for learners.

A fast intro to PostgreSQL

PostgreSQL is an open supply database, it’s obtainable for macOS, Linux and another working techniques. You’ll be able to set up it through the use of the de-facto package deal supervisor on each platform. 📦

# Linux
sudo apt-get set up postgresql postgresql-contrib
sudo service postgresql begin
# verify service standing
sudo service --status-all
sudo service postgresql standing

# macOS
brew set up postgresql
brew companies begin postgresql
# verify service standing
brew companies listing

You’ll additionally must set a correct password for the postgres person, which is the admin person by default with godlike permissions. You’ll be able to change the foundation password, you simply need to log in as a root & alter the postgres person file with the brand new move. 🔑

# Linux
sudo -u postgres psql postgres
# macOS
psql -U postgres

# psql (12.1)
# Kind "assist" for assist.
#
# postgres=#

# ALTER ROLE
alter person postgres with password 'mypassword';

# exit
q

Any longer you’ll have the ability to entry pgSQL as root on each platforms like this:

psql -h localhost -U postgres

It is suggested to make use of a devoted person for each single database that you just create as a substitute of working with a shared root person. Let me present you tips on how to create a brand new DB with an related person.

# Record of databases
l
# Present present database
choose current_database();
# Create new database
create database mydb;
# Change database
c mydb
# Create person
create person myuser with encrypted password 'mypassword';
# Grant privileges for person on the database
grant all privileges on database mydb to myuser;
# Give up from psql console
q

That’s it, you possibly can handle your database through the use of the newly created myuser account.

# Log in again to psql console with myuser utilizing mydb
psql -h localhost -U myuser mydb
# Record all tables
dt
# Describe desk construction (will likely be helpful in a while)
d+ 

You'll be able to study extra about SQL instructions utilizing this pgSQL tutorial website.

WARN: The command under can fully wipe your database, be extraordinarily cautious!

Now you're able to mess around with Fluent, however earlier than we begin I’d like to point out you some extra suggestions & methods. Throughout improvement, issues can go incorrect and also you may want a recent begin on your DB. Right here’s tips on how to drop & reinitiate the whole lot. 😱

# Reset database
c mydb
drop schema public cascade;
create schema public;
grant all on schema public to postgres;
grant all on schema public to myuser;
grant all on schema public to public;

The snippet above will delete the public schema, subsequent it’ll recreate it and add all the required permissions for the required customers. It’s fairly simple however nonetheless harmful. ⚠️

NOTE : You'll be able to execute SQL scripts straight from the terminal through the use of the next command: psql -h localhost -U myuser mydb -c "choose * from mytable;"

You'll be able to wipe the whole lot from the command line utilizing this “one-liner”:

# Run psql command from the command line
psql -h localhost -U postgres mydb
    -c "drop schema public cascade; 
    create schema public; 
    grant all on schema public to postgres; 
    grant all on schema public to myuser; 
    grant all on schema public to public;"

I favor to have every day backups from all my databases, this little shell script can do the job.

#!/bin/bash

# Backup database
BACKUP_DIR=/Customers/tib/backups
FILE_SUFFIX=_pg_backup.sql
OUTPUT_FILE=${BACKUP_DIR}/`date +"%Y_percentm_percentd__percentH_percentM"`${FILE_SUFFIX}
PGPASSWORD="mypass" pg_dump -U myuser -h localhost mydb -F p -f ${OUTPUT_FILE}
gzip $OUTPUT_FILE

# Take away outdated backups
DAYS_TO_KEEP=30
discover $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*${FILE_SUFFIX}.gz" -exec rm -rf '{}' ';'

You'll be able to simply restore a database from a backup by coming into the next strains to the terminal:

# Restore database
gunzip -k file.gz
psql -U myuser -d mydb -1 -f mybackup.sql

Typically after I restarted my mac it occurred to me that the PostgreSQL stopped working. I needed to run the snippet under to repair the difficulty. The primary line stops the service, the second initialize a brand new database, and the third will begin the service once more. Alternatively, you can begin the database once more with the brew companies begin postgresql command.

pg_ctl -D /usr/native/var/postgres cease -s -m quick
initdb /usr/native/var/postgres
pg_ctl -D /usr/native/var/postgres -l /usr/native/var/postgres/server.log begin

I’m not a DevOps guru, be at liberty to tweet me if you realize why this occurred to me. 😅

Associated posts


As a newbie server facet Swift developer you will face many obstackles. I am going to present you tips on how to keep away from the most typical ones.


Discover ways to construct a controller part that may serve fashions as JSON objects by a RESTful API written in Swift.


Get began with server-side Swift utilizing the Vapor 4 framework. Discover ways to construct a extremely easy HTTP/2 backend server.


Discover ways to implement Asynchronous JavaScript and XML (AJAX) calls utilizing Leaf templates and Vapor 4 as a server.


Practical Server Side Swift cover image

Get the Sensible Server Facet Swift e-book

Swift on the server is a tremendous new alternative to construct quick, secure and scalable backend apps. Write your very first web-based utility through the use of your favourite programming language. Discover ways to construct a modular weblog engine utilizing the most recent model of the Vapor 4 framework. This e-book will aid you to design and create fashionable APIs that'll permit you to share code between the server facet and iOS. Begin turning into a full-stack Swift developer.

Obtainable on Gumroad

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles