Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. PostgreSQL is one of the most popular object-relational database management system (shortened to ORDBMS) and is 100% open-source. It is not purely about relations anymore: PostgreSQL is more and more about NoSQL as well. The following article is a short tutorial to set up PostgreSQL 9.5 on Fedora 24, so it can be used for a development environment. For a production deployment, it is recommended to use a different set-up and harden the service.
  2.  
  3. The set of PostgreSQL database packages in Fedora’s stable repositories are almost identical to the upstream set of RPMs. There are client tools in the postgresql package. The client library in the postgresql-libs package is often required by various connectors. The most important part of the database, the daemon, is available in the postgresql-server package. Some more server-side extensions, tools, or supporting packages may be listed by running the following command in a terminal.
  4.  
  5. $ dnf list postgresql\*
  6.  
  7. Basic deployment examples may be found in the article on the Fedora wiki as well. Some first steps are also described in the Fedora Developer Portal.
  8. Basic PostgreSQL setup
  9.  
  10. Start by installing the packages, initializing the data directory, and starting the daemon.
  11.  
  12. $ sudo dnf install postgresql-server
  13. $ sudo postgresql-setup --initdb
  14. $ sudo systemctl start postgresql
  15.  
  16. Now, connect using the superuser by switching to postgres user via su. Set a password for this superuser.
  17.  
  18. $ su - postgres
  19. $ psql
  20. psql (9.5.3)
  21. Type "help" for help.
  22.  
  23. postgres=# \password postgres
  24.  
  25. Creating a user and a database
  26.  
  27. It’s not a good idea to connect to the database as postgres superuser from applications (like you don’t work as the root user in Linux all the time). For that, we’ll need a database and a separate user to access the database. Create them with the following commands.
  28.  
  29. $ createuser john -P
  30. $ createdb --owner=john mydb
  31.  
  32. We also want to limit the connections to the server from localhost only. Edit /var/lib/pgsql/data/pg_hba.conf to look like the following example below to do this.
  33.  
  34. # TYPE DATABASE USER ADDRESS METHOD
  35. host all all 127.0.0.1/32 md5
  36. host all all ::1/128 md5
  37. local all postgres peer
  38.  
  39. This configuration allows all users that provide the password (specific to PostgreSQL) to connect from the localhost. It allows the postgres user (a.k.a. the superuser) to connect in case the same user is authenticated in the operating system (su - postgres). More about this is detailed in the upstream documentation.
  40.  
  41. Now we can restart the PostgreSQL server so the changed configuration applies.
  42.  
  43. $ sudo systemctl restart postgresql
  44.  
  45. We’re all set to use the john user to access the database mydb now.
  46.  
  47. $ psql -h localhost -U john mydb
  48. Password for user john:
  49. psql (9.5.3)
  50. Type "help" for help.
  51.  
  52. mydb=> _
  53.  
  54. At this point, you can work with the database as you need: create tables, fill then with data, and so on.
  55. PostgreSQL in the container
  56.  
  57. Linux containers (especially Docker) are slowly approaching production systems. It is also not surprising there is a PostgreSQL Docker image provided by Fedora. The source is found in the Fedora-dockerfiles repository. The image is found in fedora/postgresql on Docker Hub. Starting a container for serving PostgreSQL without touching the rest of the system is easy.
  58. Install and run the Docker daemon.
  59.  
  60. $ sudo dnf install docker
  61. $ sudo systemctl start docker
  62.  
  63. Pull the image.
  64.  
  65. $ sudo docker pull fedora/postgresql
  66.  
  67. Prepare directory for data.
  68.  
  69. $ sudo mkdir data
  70. $ sudo chown 26:26 data
  71. $ sudo chcon -t svirt_sandbox_file_t data
  72.  
  73. Start the container with a few arguments. The container uses the prepared directory to store data into and creates a user and database.
  74.  
  75. $ sudo docker run -v "`pwd`/data:/var/lib/pgsql/data:Z" -e POSTGRESQL_USER=john -e POSTGRESQL_PASSWORD=secret -e POSTGRESQL_DATABASE=mydb -d -p 5432:5432 fedora/postgresql
  76.  
  77. Now you have PostgreSQL running as a container while storing data into the data directory in the current working directory.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement