当前位置:网站首页>Ubuntu installation and configuration PostgreSQL (18.04)

Ubuntu installation and configuration PostgreSQL (18.04)

2022-06-26 13:20:00 Lanterns can only come to the classroom to experience life

Write it at the front

The use of Ubuntu edition :18.04

install

  1. install postgresql

    $ sudo apt-get install postgresql
    
    • If it fails, you can update the package

      sudo apt-get update
      
  2. After successful installation , You can see psql Version of

    psql --version
    
    • If the installation is successful, output

      psql (PostgreSQL) 9.5.25
      

    It can be seen that , there PostgreSQL Version is 9.5

    After successful installation ,postgresql Automatically created : A database user postgres( No password by default )、 A data > library postgres、 One Linux user postgres

  3. Revise it Linux user postgres Password

    sudo passwd postgres
    
  4. Switch to Linux user postgres

    su postgres
    
  5. Go to the database command line

    psql
    
    • Output after successful execution

      psql (9.5.25)
      Type "help" for help.
      
      postgres=#
      
    • Postgresql The service of is enabled by default , without , You can use the following command :

      sudo /etc/init.d/postgresql start   #  Opening service 
      sudo /etc/init.d/postgresql stop    #  Close the service 
      sudo /etc/init.d/postgresql restart #  Restart the service 
      
  • see Postgresql Database directory of ( Where is the data stored )

    stay psql At the command line of , perform SHOW data_directory;

    postgres=# SHOW data_directory;
            data_directory
    ------------------------------
     /var/lib/postgresql/9.5/main
    (1 row)
    

    /var/lib/postgresql/9.5/main Is the database directory

To configure ( Optional )

To enable other computers to connect to your PostgreSQL The server

  1. Edit the file

    sudo vi /etc/postgresql/9.5/main/postgresql.conf
    

    /etc/postgresql/9.5/main/postgresql.conf

    among 9.5 yes PostgreSQL Version of , It depends on the local version

    find *#listen_addresses = 'localhost'*, It is amended as follows

    listen_addresses = '*'
    

    If you want to allow IPv4 and IPv6 Connect , Please put ‘localhost’ Replace with ‘::’

    listen_addresses = '::'
    

Modify the database directory

  1. Create a database directory ( Where you need it )

    mkdir  Directory name 
    

    For example, I created

    mkdir /home/test/pg_db
    
  2. close Postgresql service

    sudo service postgresql stop
    
  3. Copy the data ( from Default database directory Copied to the The database directory you specified

    sudo cp -rf  postgresql Default database directory for   The database directory you specified 
    

    What I do is

    sudo cp -rf /var/lib/postgresql/9.5/main /home/test/pg_db
    
  4. permissions

    sudo chown -R postgres:postgres  The database directory you specified 
    sudo chmod 700  The database directory you specified 
    

    What I do is

    sudo chown -R postgres:postgres /home/test/pg_db
    sudo chmod 700 /home/test/pg_db
    
  5. modify postgresql Of data_directory ( The database directory specified in the configuration file )

    sudo vi /etc/postgresql/9.5/main/postgresql.conf
    

    take data_directory = '/var/lib/postgresql/9.5/main' # use data in another directory It is amended as follows :

    data_directory = ' The database directory you specified '
    

    What I do is

    data_directory = '/home/test/pg_db'
    
  6. start-up Postgresql service

    sudo service postgresql start
    
原网站

版权声明
本文为[Lanterns can only come to the classroom to experience life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261131172342.html