CODENIK

Menu

PostgreSQL


PostgreSQL How to istall ad cofigure PostgreSQL in VPS.

Upload the Project Files

  1. Install PostgreSQL with the following commands:

    sudo apt update
    sudo apt install postgresql
    
    
  2. Edit the postgresql.conf file:

    sudo nano /etc/postgresql/16/main/postgresql.conf
    
    
    
  3. Locate the line:

    #listen_addresses = 'localhost'
    
  4. Uncomment it and change localhost to *:

     listen_addresses = '*'
    

    This change allows PostgreSQL to listen for connections on all available IP addresses.

  5. Edit the pg_hba.conf file:

    sudo nano /etc/postgresql/16/main/pg_hba.conf
    
  6. Add the following line to the file:

    host    all             all             0.0.0.0/0               md5
    
  7. Configure the Firewall

    sudo ufw allow 5432/tcp
    
  8. Make sure PostgreSQL is running

    sudo service postgresql start
    
    
  9. Connect to PostgreSQL

    psql -U postgres
    
    
  10. Create a new database

    CREATE DATABASE my_database;
    
    
  11. Create a user if you dont have

    CREATE USER myuser WITH PASSWORD 'mypassword';
    
    
  12. Grant All Privileges on the Database

    GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
    
    
  13. Example postgres DATABASE_URI

    postgresql://myuser:mypassword@localhost:5432/my_database