Close Menu
geekfence.comgeekfence.com
    What's Hot

    Posit AI Blog: Introducing the text package

    July 9, 2026

    Cut costs and simplify operations with writable warm storage in Amazon OpenSearch Service

    July 9, 2026

    Data centre delays expose AI cloud power limits

    July 9, 2026
    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 Read2 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

    security – How and where mnemonic/private keys are generated/stored in IOS for mobile non-custodial wallet

    July 7, 2026

    JSON All the Way Down

    July 3, 2026

    ios – HKStatisticsCollectionQuery initialResultsHandler returns nil results for one specific user — read auth granted, data exists, survives reinstall

    July 2, 2026

    Installing simulator runtimes from the command line – Donny Wals

    June 29, 2026

    SwiftText Learns to Write | Cocoanetics

    June 28, 2026

    uitabbarcontroller – UITabBar unselectedItemTintColor ignored iOS 26

    June 27, 2026
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 202560 Views

    Hard-braking events as indicators of road segment crash risk

    January 14, 202631 Views

    Redefining AI efficiency with extreme compression

    March 25, 202628 Views
    Don't Miss

    Posit AI Blog: Introducing the text package

    July 9, 2026

    AI-based language analysis has recently gone through a “paradigm shift” (Bommasani et al., 2021, p. 1),…

    Cut costs and simplify operations with writable warm storage in Amazon OpenSearch Service

    July 9, 2026

    Data centre delays expose AI cloud power limits

    July 9, 2026

    5 Settings That Make Readings Easier to See

    July 9, 2026
    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

    Posit AI Blog: Introducing the text package

    July 9, 2026

    Cut costs and simplify operations with writable warm storage in Amazon OpenSearch Service

    July 9, 2026

    Subscribe to Updates

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

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