Close Menu
geekfence.comgeekfence.com
    What's Hot

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»iOS Development»How to set up pgSQL for Fluent 4?
    iOS Development

    How to set up pgSQL for Fluent 4?

    AdminBy AdminNovember 29, 2025No Comments5 Mins Read1 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    How to set up pgSQL for Fluent 4?
    Share
    Facebook Twitter LinkedIn Pinterest Email


    2/25/20 3:20 PM
    · 2 min read


    This is a tutorial for beginners about using PostgreSQL. I’ll show you how to automatically backup and restore the database.

    If you are already familiar with PostgreSQL, but you don’t know much about how to use databases in Vapor, you should read my other tutorial about Fluent for beginners.

    A quick intro to PostgreSQL

    PostgreSQL is an open source database, it’s available for macOS, Linux and some other operating systems. You can install it by using the de-facto package manager on every platform. 📦

    # Linux
    sudo apt-get install postgresql postgresql-contrib
    sudo service postgresql start
    # check service status
    sudo service --status-all
    sudo service postgresql status
    
    # macOS
    brew install postgresql
    brew services start postgresql
    # check service status
    brew services list
    

    You’ll also need to set a proper password for the postgres user, which is the admin user by default with godlike permissions. You can change the root password, you just have to log in as a root & alter the postgres user record with the new pass. 🔑

    # Linux
    sudo -u postgres psql postgres
    # macOS
    psql -U postgres
    
    # psql (12.1)
    # Type "help" for help.
    #
    # postgres=#
    
    # ALTER ROLE
    alter user postgres with password 'mypassword';
    
    # exit
    \q
    

    From now on you’ll be able to access pgSQL as root on both platforms like this:

    psql -h localhost -U postgres
    

    It is recommended to use a dedicated user for every single database that you create instead of working with a shared root user. Let me show you how to create a new DB with an associated user.

    # List of databases
    \l
    # Show current database
    select current_database();
    # Create new database
    create database mydb;
    # Change database
    \c mydb
    # Create user
    create user myuser with encrypted password 'mypassword';
    # Grant privileges for user on the database
    grant all privileges on database mydb to myuser;
    # Quit from psql console
    \q
    

    That’s it, you can manage your database by using the newly created myuser account.

    # Log in back to psql console with myuser using mydb
    psql -h localhost -U myuser mydb
    # List all tables
    \dt
    # Describe table structure (will be useful later on)
    \d+ 

    You can learn more about SQL commands using this pgSQL tutorial site.

    The command below can completely wipe your database, be extremely careful!

    Now you are ready to play around with Fluent, but before we start I’d like to show you some more tips & tricks. During development, things can go wrong and you might need a fresh start for your DB. Here’s how to drop & reinitiate everything. 😱

    # 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, next it’ll recreate it and add all the necessary permissions for the required users. It’s pretty straightforward but still dangerous. ⚠️

    NOTE : You can execute SQL scripts straight from the terminal by using the following command: psql -h localhost -U myuser mydb -c "select * from mytable;"

    You can wipe everything from the command line using 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 prefer to have daily backups from all my databases, this little shell script can do the job.

    #!/bin/bash
    
    # Backup database
    BACKUP_DIR=/Users/tib/backups
    FILE_SUFFIX=_pg_backup.sql
    OUTPUT_FILE=${BACKUP_DIR}/`date +"%Y_%m_%d__%H_%M"`${FILE_SUFFIX}
    PGPASSWORD="mypass" pg_dump -U myuser -h localhost mydb -F p -f ${OUTPUT_FILE}
    gzip $OUTPUT_FILE
    
    # Remove old backups
    DAYS_TO_KEEP=30
    find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*${FILE_SUFFIX}.gz" -exec rm -rf '{}' ';'
    

    You can easily restore a database from a backup by entering the following lines to the terminal:

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

    Sometimes after I restarted my mac it happened to me that the PostgreSQL stopped working. I had to run the snippet below to fix the issue. The first line stops the service, the second initialize a new database, and the third will start the service again. Alternatively, you can start the database again with the brew services start postgresql command.

    pg_ctl -D /usr/local/var/postgres stop -s -m fast
    initdb /usr/local/var/postgres
    pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
    

    I’m not a DevOps guru, feel free to tweet me if you know why this happened to me. 😅


    How to set up pgSQL for Fluent 4?

    Share this article

    Thank you. 🙏

    Related posts

    How to set up pgSQL for Fluent 4?

    7/15/20 2:20 PM
    · 5 min read


    As a beginner server side Swift developer you'll face many obstackles. I'll show you how to avoid the most common ones.

    How to set up pgSQL for Fluent 4?

    4/1/20 2:20 PM
    · 10 min read


    Learn how to build a controller component that can serve models as JSON objects through a RESTful API written in Swift.

    How to set up pgSQL for Fluent 4?

    10/8/19 2:20 PM
    · 4 min read


    Get started with server-side Swift using the Vapor 4 framework. Learn how to build a really simple HTTP/2 backend server.

    How to set up pgSQL for Fluent 4?

    12/18/20 3:20 PM
    · 5 min read


    Learn how to implement Asynchronous JavaScript and XML (AJAX) calls using Leaf templates and Vapor 4 as a server.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    SwiftText | Cocoanetics

    December 28, 2025

    Uniquely identifying views – The.Swift.Dev.

    December 27, 2025

    Unable to upload my app with Transporter. Some kind of version mismatch? [duplicate]

    December 26, 2025

    Experimenting with Live Activities – Ole Begemann

    December 25, 2025

    Announcing Mastering SwiftUI for iOS 18 and Xcode 16

    December 24, 2025

    Grouping Liquid Glass components using glassEffectUnion on iOS 26 – Donny Wals

    December 22, 2025
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 20258 Views

    Microsoft 365 Copilot now enables you to build apps and workflows

    October 29, 20258 Views

    Here’s the latest company planning for gene-edited babies

    November 2, 20257 Views
    Don't Miss

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    After laying out our bold CXM predictions for 2025 and then assessing how those bets played out…

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Why Enterprise AI Scale Stalls

    December 28, 2025

    New serverless customization in Amazon SageMaker AI accelerates model fine-tuning

    December 28, 2025
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    Customer experience management (CXM) predictions for 2026: How customers, enterprises, technology, and the provider landscape will evolve 

    December 28, 2025

    What to Know About the Cloud and Data Centers in 2026

    December 28, 2025

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2025 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.